1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') {
stack.push(')');
} else if (s.charAt(i) =='{') {
stack.push('}');
} else if (s.charAt(i) == '[') {
stack.push(']');
} else {
if(stack.isEmpty()) return false;
char c = stack.pop();
if(c != s.charAt(i)) return false;
}
}
return stack.isEmpty();
}
}
|