20. Valid Parentheses
π© Easy
Question
Input: "()" "()[]{}" "(]" "([)]" "{[]}"
Output: true true false false trueStack
Complexity
Code
Last updated
π© Easy
Input: "()" "()[]{}" "(]" "([)]" "{[]}"
Output: true true false false trueLast updated
def isValid(self, s: str) -> bool:
if len(s) == 0:
return True
stack = []
for c in s:
if c == '(':
stack.append(')')
elif c == '[':
stack.append(']')
elif c == '{':
stack.append('}')
elif len(stack) == 0 or c != stack.pop():
return False
return len(stack) == 0