Skip to content

Instantly share code, notes, and snippets.

@abhinavjonnada82
Last active November 23, 2020 16:54
Show Gist options
  • Save abhinavjonnada82/f844b8646bfa4e618681177c06b911e8 to your computer and use it in GitHub Desktop.
Save abhinavjonnada82/f844b8646bfa4e618681177c06b911e8 to your computer and use it in GitHub Desktop.
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