Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean isValid(String s) {
- Stack<Character> stack = new Stack<Character>();
- 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();
- }
- }
- // s:
- // stack:
- // s: )]
- // stack: ])
- // s: )[]
- // stack:
- // s:
- // stack: )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement