# Runtime: 32 ms, faster than 39.39% of Python3 online submissions for Valid Parentheses.
# Memory Usage: 14 MB, less than 5.22% of Python3 online submissions for Valid Parentheses.
class Solution:
def isValid(self, s: str) -> bool:
hashMap = {
'(':')',
'{':'}',
'[':']'
}
stack = []
if len(s) % 2 != 0: return False
if len(s) == 0: return True
for i in range(len(s)):
if s[i] == '(' or s[i] == '{' or s[i] == '[':
stack.append(hashMap[s[i]])
else:
if not stack: return False
close_parentheses = stack.pop()
if s[i] != close_parentheses:
return False
if not stack:
return True
else:
return False