Skip to content

Commit 1fc6583

Browse files
committed
Add Solution.java to problems 0020
1 parent 2e8778f commit 1fc6583

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
Map<String,String> map;
3+
4+
public Solution() {
5+
map = new HashMap();
6+
map.put("(", ")");
7+
map.put(")", "(");
8+
map.put("{", "}");
9+
map.put("}", "{");
10+
map.put("[", "]");
11+
map.put("]", "[");
12+
}
13+
14+
public boolean isValid(String s) {
15+
Stack<String> stack = new Stack();
16+
for (int i = 0; i < s.length(); i ++) {
17+
if (!stack.isEmpty() && map.get(String.valueOf(s.charAt(i))).equals(stack.peek())) {
18+
stack.pop();
19+
} else {
20+
stack.push(String.valueOf(s.charAt(i)));
21+
}
22+
}
23+
return stack.isEmpty();
24+
}
25+
}

0 commit comments

Comments
 (0)