Last active
December 14, 2015 10:09
-
-
Save ylegall/5070055 to your computer and use it in GitHub Desktop.
Balanced Smileys. (problem from the Facebook Hacker Cup 2013 Hackathon.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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