Skip to content

Commit bc06058

Browse files
committed
1 parent d017cf2 commit bc06058

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

python/20-Valid-Parentheses.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Solution:
22
def isValid(self, s: str) -> bool:
3-
Map = {")": "(", "]": "[", "}": "{"}
43
stack = []
5-
6-
for c in s:
7-
if c not in Map:
8-
stack.append(c)
9-
continue
10-
if not stack or stack[-1] != Map[c]:
11-
return False
12-
stack.pop()
13-
14-
return not stack
4+
mapping = {'}':'{', ']':'[', ')':'('}
5+
for letter in s:
6+
if letter in mapping:
7+
if not stack or stack.pop() != mapping[letter]:
8+
return False
9+
else:
10+
stack.append(letter)
11+
if not stack:
12+
return True

0 commit comments

Comments
 (0)