Advertisement
AlimusSifar

Matching Brackets - Toph.co - Stack

Apr 29th, 2020
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. text = input()
  2. stack = []; match = 0
  3. for i in text:
  4.     if i == '(' or i == '{' or i == '[':
  5.         stack.append(i)
  6.     elif len(stack) > 0:
  7.         pop = stack.pop()
  8.         if ord(i) == ord(pop)+1 or ord(i) == ord(pop)+2:
  9.             match += 1
  10.  
  11. if match == len(text)/2:
  12.     print('Yes')
  13. else:
  14.     print('No')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement