Solution to LeetCode Valid Parentheses problem.
We use a stack to check if the parentheses are valid.
public boolean isValid(String S) {
Stack<String> s = new Stack<String>();
for(char c : S.toCharArray()){
String cc = String.valueOf(c);
if(c == '{' || c == '(' || c == '[') s.push(cc);
else{
if(s.empty()) return false;
char top = s.peek().toCharArray()[0];
if( (top == '{' && c == '}') ||
(top == '(' && c == ')') ||
(top == '[' && c == ']') ){
s.pop();
} else return false;
}
}
return s.empty();
}