Skip to content

Instantly share code, notes, and snippets.

@twsiyuan
Last active December 23, 2021 10:49
Show Gist options
  • Save twsiyuan/4d2f5565fedb9d4984199dcd7f6642c6 to your computer and use it in GitHub Desktop.
Save twsiyuan/4d2f5565fedb9d4984199dcd7f6642c6 to your computer and use it in GitHub Desktop.
LeetCode #20
# https://leetcode.com/problems/valid-parentheses/
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for c in s:
if c == ")" or c == "]" or c == "}":
if len(stack) <= 0:
return False
else:
c2 = stack.pop()
if not((c == ")" and c2 == "(") or (c == "]" and c2 == "[") or (c == "}" and c2 == "{")):
stack.append(c2)
stack.append(c)
else:
stack.append(c)
return len(stack) <= 0
@QureshiAbraham
Copy link

It is a best solution found that very popular and helpful:
https://www.youtube.com/watch?v=-3S5gH2cuYw&ab_channel=EricProgramming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment