| 10 | */ |
| 11 | public class Solution { |
| 12 | public boolean isValid(String s) { |
| 13 | char[] stack = new char[s.length() + 1]; |
| 14 | int top = 1; |
| 15 | for (char c : s.toCharArray()) { |
| 16 | if (c == '(' || c == '[' || c == '{') { |
| 17 | stack[top++] = c; |
| 18 | } else if (c == ')' && stack[--top] != '(') { |
| 19 | return false; |
| 20 | } else if (c == ']' && stack[--top] != '[') { |
| 21 | return false; |
| 22 | } else if (c == '}' && stack[--top] != '{') { |
| 23 | return false; |
| 24 | } |
| 25 | } |
| 26 | return top == 1; |
| 27 | } |
| 28 | |
| 29 | public static void main(String[] args) { |
| 30 | Solution solution = new Solution(); |