> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/stacks-and-queues/generate-all-parentheses.md).

# 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

```java
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;
    }
}
```
