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 a5769ca commit 2ffbb37Copy full SHA for 2ffbb37
1 file changed
valid-parentheses/gyeo-ri.py
@@ -1,31 +1,19 @@
1
-from collections import deque
2
-
3
4
class Solution:
5
def isValid(self, s: str) -> bool:
6
- character_map = {
7
- "(": ")",
8
- "{": "}",
9
- "[": "]",
10
- }
11
- s_stack = deque()
+ character_map = {"(": ")", "{": "}", "[": "]"}
+ s_list = []
12
13
for c in s:
14
- if c in character_map.keys():
15
- s_stack.append(c)
+ if c in character_map:
+ s_list.append(character_map[c])
16
17
else:
18
- if len(s_stack) > 0:
19
- previous = s_stack.pop()
20
- if character_map[previous] != c:
21
- return False
22
- else:
23
+ if len(s_list) > 0:
+ if s_list.pop() == c:
+ continue
+ return False
24
25
26
27
28
- return True
+ return len(s_list) == 0
29
30
31
if __name__ == "__main__":
0 commit comments