Skip to content

Instantly share code, notes, and snippets.

@ksamirdev
Created April 23, 2026 14:05
Show Gist options
  • Select an option

  • Save ksamirdev/9d72b8d978c2b1e1b3e8a0e508ac9f11 to your computer and use it in GitHub Desktop.

Select an option

Save ksamirdev/9d72b8d978c2b1e1b3e8a0e508ac9f11 to your computer and use it in GitHub Desktop.
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
closeToOpen = {
")" : "(",
"]": "[",
"}": "{"
}
for c in s:
if c in closeToOpen:
if stack and stack[-1] == closeToOpen[c]:
stack.pop()
else:
return False
else:
stack.append(c)
return len(stack) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment