Generate all Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

public class Solution {
    public int isValid(String A) {
        Stack<Character> st = new Stack<>();
        for (int i = 0; i < A.length(); i++) {
            if (st.size() == 0)
                st.push(A.charAt(i));
            else if ((A.charAt(i) == ']' && st.peek() == '[') || (A.charAt(i) == '}' && st.peek() == '{')
                    || (A.charAt(i) == ')' && st.peek() == '('))
                st.pop();
            else
                st.push(A.charAt(i));
        }
        if (st.size() == 0)
            return 1;
        return 0;
    }
}

Last updated