Skip to content

Commit e432fbf

Browse files
committed
valid prentheses
1 parent c2c0227 commit e432fbf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

valid-parentheses/se6816.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
Stack을 활용한 방식
3+
문자열 s의 길이 -> N
4+
시간 복잡도 : O(N)
5+
공간 복잡도 : O(N)
6+
*/
7+
class Solution {
8+
public boolean isValid(String s) {
9+
boolean result= true;
10+
Stack<Character> sta=new Stack<>();
11+
Map<Character,Character> map=new HashMap<>();
12+
map.put('}','{');
13+
map.put(']','[');
14+
map.put(')','(');
15+
for(int i=0; i<s.length();i++){
16+
char ch=s.charAt(i);
17+
if(ch == '(' || ch == '[' || ch == '{'){
18+
sta.push(ch);
19+
continue;
20+
}
21+
char target=map.get(ch);
22+
if(sta.isEmpty()){
23+
result=false;
24+
break;
25+
}
26+
if(!(sta.peek() == target)){
27+
result=false;
28+
break;
29+
}
30+
sta.pop();
31+
}
32+
if(!sta.isEmpty()){
33+
result=false;
34+
}
35+
return result;
36+
}
37+
}

0 commit comments

Comments
 (0)