Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created January 20, 2026 08:10
Show Gist options
  • Select an option

  • Save maehrm/fb788568888f604e7a0f84e42d2e4d40 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/fb788568888f604e7a0f84e42d2e4d40 to your computer and use it in GitHub Desktop.
def solv():
stack = []
c_set = set()
for c in S:
if c == "(":
stack.append(c)
elif c == ")":
while stack and stack[-1] != "(":
ch = stack.pop()
c_set.remove(ch)
stack.pop()
else:
if c in c_set:
return False
c_set.add(c)
stack.append(c)
return True
S = list(input())
print("Yes") if solv() else print("No")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment