From 756a6a01cca3bec571eaf1d4e8af45492a1a6042 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Fri, 21 Jul 2023 16:35:53 -0700 Subject: [PATCH] first attempt at generate strings, need filter for permutations --- Top150Interview/22_Generate_Parenthesis.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Top150Interview/22_Generate_Parenthesis.py b/Top150Interview/22_Generate_Parenthesis.py index fc5f45a..b9f9201 100644 --- a/Top150Interview/22_Generate_Parenthesis.py +++ b/Top150Interview/22_Generate_Parenthesis.py @@ -1,7 +1,25 @@ -def generateParenthesis(n): +def generateParenthesis(n: int) -> List[str]: """ :type n: int :rtype: List[str] """ + stack = [] + pairs = "" + opening_brackets = {'('} + closing_brackets = {')'} + bracket_pairs = {')': '('} + + # Create n pairs + for i in range(n): + pairs += "()" + # print(pairs) + # Create and filter permutations + for pair in permutations(pairs): + parenthesis = "".join(pair) + # print(pair) + # print(parenthesis) + if parenthesis in bracket_pairs.keys(): + if not stack or stack[-1] != bracket_pairs[parenthesis]: + print('bad') generateParenthesis(3) \ No newline at end of file