/ leetcodesolve / validparentatis.py
swadhinbiswas test commit · 3/25/2024
74b485a
27 lines · 593 Bytes
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
27
class Solution:
    def isValid(self, s: str) -> bool:
      stack=[]
      openbracket={
        '[':1,'{':2,'(':3,
      }
      closebracket={
        ']':1,'}':2,')':3,
      }
      
      for i in s:
        if i not in closebracket:
          stack.append(i)
        else:
          if len(stack)>0:
            pop_brack=stack.pop()
            if closebracket[i]==openbracket[pop_brack]:
              continue
            else:
              return False
          else:
            return False
          
      if len(stack)==0:
        return True
      else:
        return False