Advertisement
Derga

Untitled

Aug 22nd, 2023
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. def match(a, b):
  2.     if a == '(' and b == ')':
  3.         return True
  4.     if a == '[' and b == ']':
  5.         return True
  6.     if a == '{' and b == '}':
  7.         return True
  8.     return False
  9.  
  10.  
  11. s = input()
  12. stack = list()
  13. for i in s:
  14.     if i in ('(', '[', '{'):
  15.         stack.append(i)
  16.     else:
  17.         if len(stack) == 0 or not match(stack[-1], i):
  18.             print('NO')
  19.             exit()
  20.         else:
  21.             stack.pop()
  22.  
  23. if len(stack) == 0:
  24.     print('YES')
  25. else:
  26.     print('NO')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement