All Courses

Balanced Parentheses Check problem

By Subham, 2 years ago
  • Bookmark

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
  return chars == []     




Balanced parentheses
1 Answer

Solution :

First we will scan the string from left to right, and every time we see an opening parenthesis we push it to a stack, because we want the last opening parenthesis to be closed first. (Remember the FILO structure of a stack!)

Then, when we see a closing parenthesis we check whether the last opened one is the corresponding closing match, by popping an element from the stack. If it’s a valid match, then we proceed forward, if not return false.

Or if the stack is empty we also return false, because there’s no opening parenthesis associated with this closing one. In the end, we also check whether the stack is empty. If so, we return true, otherwise return false because there were some opened parenthesis that were not closed.

def balance_check(s):
  # Check is even number of brackets
  if len(s)%2 != 0:
    return False
  # Set of opening brackets
  opening = set('([{') 
  # Matching Pairs
  matches = set([ ('(',')'), ('[',']'), ('{','}') ]) 
  # Use a list as a "Stack"
  stack = []
  # Check every parenthesis in string
  for paren in s:
    # If its an opening, append it to list
    if paren in opening:
      # Check that there are parentheses in Stack
      if len(stack) == 0:
        return False
      # Check the last open parenthesis
      last_open = stack.pop()
      # Check if it has a closing match
      if (last_open,paren) not in matches:
        return False
  return len(stack) == 0




Your Answer


Why You Should Learn Data Science in 2023?

Jun 8th (7:00 PM) 289 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More