We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def isBalanced(s):
# Write your code here
stk = []
for i in s:
if i == '(' or i == '{' or i == '[':
stk.append(i)
else:
if len(stk) == 0:
return "NO"
elif i == ')' and stk[-1] == '(':
stk.pop()
elif i == '}' and stk[-1] == '{':
stk.pop()
elif i == ']' and stk[-1] == '[':
stk.pop()
else:
return "NO"
if len(stk)>0:
return "NO"
return "YES"
return "NO"
if len(stk)>0:
return "NO"
return "YES"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
def isBalanced(s): # Write your code here stk = [] for i in s: if i == '(' or i == '{' or i == '[': stk.append(i) else: if len(stk) == 0: return "NO" elif i == ')' and stk[-1] == '(': stk.pop() elif i == '}' and stk[-1] == '{': stk.pop() elif i == ']' and stk[-1] == '[': stk.pop() else: return "NO"