From 1c16e558445095a4774578f5671f37acc42c5f59 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Mon, 24 Jul 2023 22:44:11 -0700 Subject: [PATCH] first iteration, passes 30/93 --- Top150Interview/20_Valid_Parenthesis.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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