Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean isValid(String s) {
- if (s == null || s.length() % 2 != 0) {
- return false;
- }
- Stack<Character> stack = new Stack<>();
- for (Character curCh : s.toCharArray()) {
- if (curCh == '(' || curCh == '[' || curCh == '{') {
- stack.push(curCh);
- } else {
- if (stack.isEmpty()) return false;
- char ch = stack.pop();
- if ((curCh == ')' && ch != '(') || (curCh == '}' && ch != '{') || (curCh == ']' && ch != '[')) {
- return false;
- }
- }
- }
- return stack.isEmpty();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement