-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-20-Valid-Parentheses.java
35 lines (30 loc) · 1.08 KB
/
LeetCode-20-Valid-Parentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
LeetCode: https://leetcode.com/problems/valid-parentheses/
LintCode: http://www.lintcode.com/problem/valid-parentheses/
JiuZhang: http://www.jiuzhang.com/solutions/valid-parentheses/
ProgramCreek: http://www.programcreek.com/2012/12/leetcode-valid-parentheses-java/
Analysis:
A typical problem solved by using stack.
*/
public class Solution {
public boolean isValid(String s) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('(', ')');
map.put('{', '}');
map.put('[', ']');
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
char curr = s.charAt(i);
if(map.keySet().contains(curr)){
stack.push(curr);
}else if(map.values().contains(curr)){
if(!stack.empty() && map.get(stack.peek()) == curr){
stack.pop();
}else{
return false;
}
}
}
return stack.empty();
}
}