Skip to main content

20 - Valid Parentheses

Details

KeyValue
Linkhttps://leetcode.com/problems/valid-parentheses/
LanguagePython 3
Runtime59 ms, faster than 16.02% of Python3 online submissions for Valid Parentheses
Memory Usage13.9 MB, less than 24.44% of Python3 online submissions for Valid Parentheses
Datastructuresstack
Algorithmsiterator

Procedure

  1. TBD...

Code

class Solution:
def isValid(self, s: str) -> bool:
stack = []
map = { ')':'(', '}':'{', ']':'[' }

for char in s:
if char in map:
if stack and stack[-1] == map[char]:
stack.pop()
else:
return False
else:
stack.append(char)

return not stack