diff --git a/Top150Interview/20_Valid_Parenthesis.py b/Top150Interview/20_Valid_Parenthesis.py index e488cc8..54f8d9f 100644 --- a/Top150Interview/20_Valid_Parenthesis.py +++ b/Top150Interview/20_Valid_Parenthesis.py @@ -1,6 +1,25 @@ -def isValid(self, s): +def isValid(s): """ :type s: str :rtype: bool """ - \ No newline at end of file + pervious = "" + openBracket = ['(', '{', '['] + bracketPairs = ['()', '{}', '[]'] + pairs = None + + for x in s: + if x in openBracket: + previous = x + else: + # print(previous+x) + if (previous+x) in bracketPairs: + pairs = True + else: + return False + return pairs + +print(isValid("()")) +print(isValid("()[]{}")) +print(isValid("(]")) +print(isValid("([)]")) \ No newline at end of file