We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c2c0227 commit e432fbfCopy full SHA for e432fbf
valid-parentheses/se6816.java
@@ -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
28
29
30
+ sta.pop();
31
32
+ if(!sta.isEmpty()){
33
34
35
+ return result;
36
37
+}
0 commit comments