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.
stack = []
pairs = {
'[': ']',
'{': '}',
'(': ')'
}
for bracket in s:
if bracket in pairs:
stack.append(bracket)
elif not stack or pairs.get(stack.pop(-1)) != bracket:
return 'NO'
return 'YES' if not stack else 'NO'
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 →
Python 3:
Write your code here