LeetCode/Top150Interview/22_Generate_Parenthesis.py

25 lines
644 B
Python

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)