Skip to content

Commit c25e40a

Browse files
committed
week 6 - valid parentheses (추가 풀이)
1 parent 0e45f5d commit c25e40a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

valid-parentheses/liza0525.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,25 @@ def isValid(self, s: str) -> bool:
3939

4040
# 위의 모든 invalid한 경우를 통과했다면 valid하다고 할 수 있다.
4141
return True
42+
43+
# 리뷰 반영 풀이
44+
# 괄호의 쌍을 key-value로 하는 pairs 딕셔너리를 이용하여, if 조건을 더욱 간단하게 할 수 있음
45+
def isValid(self, s: str) -> bool:
46+
stack = []
47+
pairs = {")": "(", "}": "{", "]": "["}
48+
49+
for ss in s:
50+
if ss in ('(', '[', '{'):
51+
stack.append(ss)
52+
else:
53+
if not stack:
54+
return False
55+
56+
last_ss = stack[-1]
57+
if last_ss == pairs[ss]:
58+
stack.pop()
59+
else:
60+
return False
61+
if stack:
62+
return False
63+
return True

0 commit comments

Comments
 (0)