[Link] https://leetcode.com/problems/valid-parentheses/
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(' || c== '{' || c == '[') stack.push(c);
else if(stack.isEmpty() || stack.pop() != c - (c == ')' ? 1 : 2)) return false;
}
return stack.isEmpty() ? true : false;
}
}