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 01a553b commit 6c5814eCopy full SHA for 6c5814e
valid-parentheses/sungjinwi.py
@@ -1,3 +1,18 @@
1
+"""
2
+ 풀이 :
3
+ stack구조를 이용해서 구현
4
+ 괄호의 페어를 딕셔너리의 key, value를 이용해 매치시킨다
5
+ 여는 괄호를 만나면 stack에 push, 닫는 괄호를 만나면 stack의 마지막 괄호의 pair일 때 pop, 아니면 return False
6
+ 문자열 전체를 순회했을 때 stack에 남아있으면 안 닫힌 괄호가 있으므로 return False
7
+
8
+ s의 길이 N
9
10
+ TC : O(N)
11
+ s에 대해 한번 for문
12
+ SC : O(N)
13
+ stack의 최대 크기는 s의 길이와 비례
14
15
16
class Solution:
17
def isValid(self, s: str) -> bool:
18
stack = []
@@ -7,11 +22,9 @@ def isValid(self, s: str) -> bool:
22
stack.append(paren)
23
elif not stack :
24
return False
- elif pair[stack[-1]] == paren :
- stack.pop()
- else:
25
+ elif pair[stack.pop()] != paren :
26
27
if not stack :
28
return True
29
else :
- return False
30
+ return False
0 commit comments