All Courses

Balanced Parentheses Check problem

By Subham, 3 years ago
  • Bookmark
1

Given a string of opening and closing parentheses, check whether it’s balanced. We have 3 types of parentheses: round brackets: (), square brackets: [], and curly brackets: {}. Assume that the string doesn’t contain any other character than these, no spaces words or numbers. As a reminder, balanced parentheses require every opening parenthesis to be closed in the reverse order opened. For example ‘([])’ is balanced but ‘([)]’ is not.

You can assume the input string has no spaces.


Fill out your solution below:

def balance_check(s):
  chars = []
  matches = {')':'(',']':'[','}':'{'}
  for c in s:
    if c in matches:
      if chars.pop() != matches[c]:
        return False
    else:
      chars.append(c)
  return chars == []     
  pass

balance_check('[]')

balance_check('[](){([[[]]])}')

balance_check('()(){]}')

Balanced parentheses
0 Answer
Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More