Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.out.println(validate("()")); #true
- System.out.println(validate("[[()]]{[]}")); #true
- System.out.println(validate("[[(])]{[]}")); #false
- private static boolean validate(String s) {
- Stack<Character> stack = new Stack<>();
- for(int i = 0;i<s.length();i++) {
- switch(s.charAt(i)) {
- case '(': stack.push(')');break;
- case '[': stack.push(']');break;
- case '{': stack.push('}');break;
- //
- case ')': {
- if(!stack.pop().equals(')'))
- return false;
- break;
- }
- case ']': {
- if(!stack.pop().equals(']'))
- return false;
- break;
- }
- case '}': {
- if(!stack.pop().equals('}'))
- return false;
- break;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement