Skip to content

Commit 4102d1a

Browse files
committed
add solution of valid-parentheses
1 parent 2e944b3 commit 4102d1a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
class Solution {
3+
public boolean isValid(String s) {
4+
5+
Deque<Character> stack = new ArrayDeque<>();
6+
Map<Character, Character> table = new HashMap<>();
7+
table.put(')', '(');
8+
table.put(']', '[');
9+
table.put('}', '{');
10+
11+
for (int i = 0; i < s.length(); i++) {
12+
if (table.containsKey(s.charAt(i))) {
13+
14+
if ((table.get(s.charAt(i))).equals(stack.peek())) {
15+
stack.pop();
16+
} else {
17+
stack.push(s.charAt(i));
18+
}
19+
20+
} else {
21+
stack.push(s.charAt(i));
22+
}
23+
}
24+
25+
return stack.isEmpty();
26+
}
27+
}

0 commit comments

Comments
 (0)