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.
- Balanced Brackets
- Discussions
Balanced Brackets
Balanced Brackets
+ 0 comments There's probably a more efficient solution than this, but this worked for me (in Python).
x = s if len(x) % 2 != 0: return 'NO'
a = '()' b = '[]' c = '{}' while a in x or b in x or c in x: x = x.replace(a, '') x = x.replace(b, '') x = x.replace(c, '') if len(x) != 0: return 'NO' return 'YES'
+ 0 comments Try to make it clear with python:
def isBalanced(s): if s[0] in ['}', ']', ')']: return 'NO' dic = {'}':'{', ']':'[', ')':'('} stack = [] for i in s: if i in ['{', '[', '(']: stack.append(i) else: if not stack: return 'NO' value = stack.pop() if value != dic[i]: return 'NO' if stack: return 'NO' return 'YES'
+ 0 comments Yet another python. I was sure I'll find here much more sophisticated code when finally complete the task by myself and I was right :D Anyway, this is my solution:
def isBalanced(s): # Write your code here print(s) tempTab = [] tempValue = "" if (len(s) % 2) != 0: # check if every bracket has a pair return "NO" if (s[0] == '(' or s[0] == '{' or s[0] == '['): # check first element tempTab.append(s[0]) else: return "NO" for x in range(1, len(s)): # check the rest of elements if (s[x] == '(' or s[x] == '{' or s[x] == '['): # if opening bracket, add it to the list tempTab.append(s[x]) else: # otherwise, check if the last opening bracket fits to the current closing bracket try: tempValue = tempTab.pop() # remove the last opening except IndexError: return "NO" if s[x] == ')': if (tempValue == '('): pass else: return 'NO' elif s[x] == '}': if (tempValue == '{'): pass else: return 'NO' elif s[x] == ']': if (tempValue == '['): pass else: return 'NO' if len(tempTab) == 0: # finally, check if all the opening brackets have been closed return 'YES' else: return 'NO'
+ 0 comments My Python solution using a stack:
def isBalanced(s: str) -> str: def match_bracket(c, stack): c_match = stack.get() if c=='}' and c_match=='{': return stack elif c==']' and c_match=='[': return stack elif c==')' and c_match=='(': return stack else: stack.put(c_match) stack.put(c) return stack s_stack = LifoQueue() for c in s: if s_stack.empty(): s_stack.put(c) continue else: s_stack = match_bracket(c, s_stack) return 'YES' if s_stack.empty() else 'NO'
+ 0 comments function isBalanced(s) { // Write your code here let brackets = s.split('') let closedBrackets = [] let currentBraket let closedBracket while(brackets.length > 0){ currentBraket = brackets.pop() // console.log(brackets,currentBraket) if(currentBraket === ')' | currentBraket === '}'| currentBraket === ']'){ closedBrackets.push(currentBraket) }else{ if(closedBrackets.length == 0){ return 'NO' }else{ closedBracket = closedBrackets.pop() if(currentBraket == '(' && closedBracket !== ')') return 'NO' if(currentBraket == '{' && closedBracket !== '}') return 'NO' if(currentBraket == '[' && closedBracket !== ']') return 'NO' } } } if(closedBrackets.length > 0) return 'NO' return 'YES' }
Load more conversations
Sort 184 Discussions, By:
Please Login in order to post a comment