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 d017cf2 commit bc06058Copy full SHA for bc06058
python/20-Valid-Parentheses.py
@@ -1,14 +1,12 @@
1
class Solution:
2
def isValid(self, s: str) -> bool:
3
- Map = {")": "(", "]": "[", "}": "{"}
4
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
+ mapping = {'}':'{', ']':'[', ')':'('}
+ for letter in s:
+ if letter in mapping:
+ if not stack or stack.pop() != mapping[letter]:
+ return False
+ else:
+ stack.append(letter)
+ if not stack:
+ return True
0 commit comments