Skip to content

Commit

Permalink
solved leetcode 20, valid parentheses.python3 -u /Users/iamserda/repo…
Browse files Browse the repository at this point in the history
…s/SWENG/neetcodeio/algostructybeginners/Lv1-Arrays/valid_parentheses.py

-@iamserda
  • Loading branch information
iamserda committed Jan 22, 2025
1 parent de36e7c commit 74afeb1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions neetcodeio/algostructybeginners/Lv1-Arrays/valid_parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def isValid(string: str) -> bool:
openers = set(list("{[("))
matching_set = {"{}", "[]", "()"}
stack = []

for char in string:
if char in openers:
stack.append(char)
continue
if not stack:
return False
if stack[-1] + char in matching_set:
stack.pop()
continue
return False

if len(stack) == 0:
return True
return False


assert isValid("[") == False
assert isValid("()") == True
assert isValid("()[]{}") == True
assert isValid("(]") == False
assert isValid("([])") == True

0 comments on commit 74afeb1

Please sign in to comment.