Skip to content

Commit 2ffbb37

Browse files
committed
fix: 로직은 유지한 채로 가독성 / 메모리 사용을 개선
1 parent a5769ca commit 2ffbb37

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

valid-parentheses/gyeo-ri.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
from collections import deque
2-
3-
41
class Solution:
52
def isValid(self, s: str) -> bool:
6-
character_map = {
7-
"(": ")",
8-
"{": "}",
9-
"[": "]",
10-
}
11-
s_stack = deque()
3+
character_map = {"(": ")", "{": "}", "[": "]"}
4+
s_list = []
125

136
for c in s:
14-
if c in character_map.keys():
15-
s_stack.append(c)
7+
if c in character_map:
8+
s_list.append(character_map[c])
169

1710
else:
18-
if len(s_stack) > 0:
19-
previous = s_stack.pop()
20-
if character_map[previous] != c:
21-
return False
22-
else:
23-
return False
11+
if len(s_list) > 0:
12+
if s_list.pop() == c:
13+
continue
14+
return False
2415

25-
if len(s_stack) > 0:
26-
return False
27-
else:
28-
return True
16+
return len(s_list) == 0
2917

3018

3119
if __name__ == "__main__":

0 commit comments

Comments
 (0)