Skip to content

Commit 6c5814e

Browse files
committed
#222 valid-parentheses calculate complexity
1 parent 01a553b commit 6c5814e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

valid-parentheses/sungjinwi.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
116
class Solution:
217
def isValid(self, s: str) -> bool:
318
stack = []
@@ -7,11 +22,9 @@ def isValid(self, s: str) -> bool:
722
stack.append(paren)
823
elif not stack :
924
return False
10-
elif pair[stack[-1]] == paren :
11-
stack.pop()
12-
else:
25+
elif pair[stack.pop()] != paren :
1326
return False
1427
if not stack :
1528
return True
1629
else :
17-
return False
30+
return False

0 commit comments

Comments
 (0)