repl.it
@manoj2rox/

Balanced Parenthesis Check

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Logic is appending open brackets to a list. when closed braces arrives, we'll pop out the open bracket

def balanced_paren(str):
  #Edge case Check
  if len(str)%2 != 0:
    return False
  
  #open brackets
  opening = set('([{')
  #Matching brackets
  matches = set([('(',')'),('[',']'),('{','}')])
  stack = []

  for paren in str:
    if paren in opening:
      stack.append(paren)
    else:
      if len(stack) == 0:
        return False

      last_open = stack.pop()

      if(last_open,paren) not in matches:  
        return False
        
  return len(stack) == 0
?