Last active
November 23, 2020 16:54
-
-
Save abhinavjonnada82/f844b8646bfa4e618681177c06b911e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| HashMap<Character, Character> mappings; | |
| public Solution() { | |
| this.mappings = new HashMap<Character, Character>(); | |
| this.mappings.put(')', '('); | |
| this.mappings.put('}', '{'); | |
| this.mappings.put(']', '['); | |
| } | |
| public boolean isValid(String s) { | |
| 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 (c == '}' && !stack.isEmpty() && stack.peek() == '{') { | |
| stack.pop(); | |
| } | |
| else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') { | |
| stack.pop(); | |
| } | |
| } | |
| return stack.isEmpty(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment