You are viewing a single comment's thread. Return to all comments →
Feel free to change the brackets variables to a dictionary. For this case scenario, there should not be a huge difference in performance.
from collections import deque def isBalanced(s): open_list = deque() l_brac = ('(', '{', '[') r_brac = (')', '}', ']') for bracket in s: if bracket in l_brac: open_list.append(r_brac[l_brac.index(bracket)]) continue if len(open_list) > 0 and open_list[-1] == bracket: open_list.pop() continue return "NO" return "YES" if len(open_list) == 0 else "NO"
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
Feel free to change the brackets variables to a dictionary. For this case scenario, there should not be a huge difference in performance.