Skip to content

Commit 1bb6e1b

Browse files
committed
valid-parentheses
1 parent 9194aa9 commit 1bb6e1b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

valid-parentheses/taekwon-dev.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
class Solution {
6+
public boolean isValid(String s) {
7+
Map<Character, Character> map = new HashMap<>(){{
8+
put(')', '(');
9+
put('}', '{');
10+
put(']', '[');
11+
}};
12+
13+
Deque<Character> stack = new ArrayDeque<>();
14+
15+
for (int i = 0; i < s.length(); i++) {
16+
char c = s.charAt(i);
17+
18+
if (!map.containsKey(c)) {
19+
stack.push(c);
20+
continue;
21+
}
22+
23+
if (stack.isEmpty() || stack.removeFirst() != map.get(c)) {
24+
return false;
25+
}
26+
}
27+
28+
return stack.size() == 0;
29+
}
30+
}

0 commit comments

Comments
 (0)