Skip to content

Instantly share code, notes, and snippets.

@ylegall
Last active December 14, 2015 10:09
Show Gist options
  • Save ylegall/5070055 to your computer and use it in GitHub Desktop.
Save ylegall/5070055 to your computer and use it in GitHub Desktop.
Balanced Smileys. (problem from the Facebook Hacker Cup 2013 Hackathon.)
def is_balanced(msg, pos = 0, stack = 0):
for i in range(pos, len(msg)):
c = msg[i]
if c == '(': stack += 1
elif c == ')':
stack -= 1
if stack < 0: return False
elif c == ':':
if i < len(msg)-1:
if msg[i + 1] == '(':
if is_balanced(msg, i+2, stack): return True
elif msg[i + 1] == ')':
if is_balanced(msg, i+2, stack): return True
return stack == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment