20. Valid Parentheses
Input: "()"
Output: trueInput: "()[]{}"
Output: trueInput: "(]"
Output: falseInput: "([)]"
Output: falseLast updated
Input: "()"
Output: trueInput: "()[]{}"
Output: trueInput: "(]"
Output: falseInput: "([)]"
Output: falseLast updated
Input: "{[]}"
Output: true public boolean isValid(String s) {
if(s.length() % 2 == 1) return false;
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()) {
if (c == '(') stack.push(')');
else if (c == '{') stack.push('}');
else if (c == '[') stack.push(']');
else if (stack.isEmpty() || stack.pop() != c) return false;
}
return stack.isEmpty();
}