Skip to content

Commit

Permalink
Create valid-parentheses.py
Browse files Browse the repository at this point in the history
not the fastest but works for this one
  • Loading branch information
gabedonnan authored Jan 18, 2023
1 parent c16d7ec commit 32ac792
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions valid-parentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for letter in s:
if (stack[-1] if len(stack) > 0 else "") == self.other(letter):
stack.pop()
else:
stack.append(letter)
return len(stack) == 0

def other(self, ch: str) -> str:
return {"{": None, "}": "{", "(": None, ")": "(", "[": None, "]": "["}[ch]

0 comments on commit 32ac792

Please sign in to comment.