Given a string containing just the characters
'('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order,
"()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.Solution:
If we find a left parenthesis, we push its right parenthesis into the stack.
If we find a non-left parenthesis character, we pop the stack and check if there's a match. If not, simply return false.
Code:
public class Solution { public boolean isValid(String s) { 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(); } }