Valid Parentheses

Problem Link: https://leetcode.com/problems/valid-parentheses/

class Solution:
    def isValid(self, string: str) -> bool:
        
        s=[]
        for i in string:
            if(i=='(' or i=='[' or i=='{'):
                s.append(i)
            else:
                if(i==']'):
                    if(len(s)==0 or s[-1]!='['):
                        return False
                    else:
                        s.pop()
                elif(i==')'):
                    if(len(s)==0 or s[-1]!='('):
                        return False
                    else:
                        s.pop()
                elif(i=='}'):
                    if(len(s)==0 or s[-1]!='{'):
                        return False
                    else:
                        s.pop()
        if(len(s)==0):
            return True
        else:
            return False

Last updated