0020. Valid Parentheses
Description
**Input:** s = "()"
**Output:** true**Input:** s = "()[]{}"
**Output:** true**Input:** s = "(]"
**Output:** false**Input:** s = "([)]"
**Output:** falseac
Last updated
**Input:** s = "()"
**Output:** true**Input:** s = "()[]{}"
**Output:** true**Input:** s = "(]"
**Output:** false**Input:** s = "([)]"
**Output:** falseLast updated
**Input:** s = "{[]}"
**Output:** trueclass Solution {
public boolean isValid(String s) {
// edge cases
if (s == null || s.length() <= 1) return false;
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()) return false;
char p = stack.pop();
if (c == ')' && p == '(' || c == '}' && p == '{' || c == ']' && p == '[') {
continue;
} else {
return false;
}
}
}
return stack.isEmpty();
}
}