-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_generateParenthesis.py
69 lines (56 loc) · 1.91 KB
/
22_generateParenthesis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 22. Generate Parentheses
# Refer to Nick White's video.
# Solution with Backtracking.
def backtrack(res, curr, open_, close_, n):
if len(curr)==n*2:
res.append(curr)
return
if open_<n:
backtrack(res, curr+"(", open_+1, close_, n)
if close_<open_:
backtrack(res, curr+")", open_, close_+1, n)
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
backtrack(res, "", 0, 0, n)
return res
# # Solution without backtracking
# class Solution:
# def generateParenthesis(self, n):
# def generate(A = []):
# if len(A) == 2*n:
# if valid(A):
# ans.append("".join(A))
# else:
# A.append('(')
# generate(A)
# A.pop()
# A.append(')')
# generate(A)
# A.pop()
# def valid(A):
# bal = 0
# for c in A:
# if c == '(': bal += 1
# else: bal -= 1
# if bal < 0: return False
# return bal == 0
# ans = []
# generate()
# return ans
# LAME ATTEMPT(WRONG SOLUTION)
# class Solution:
# def generateParenthesis(self, n: int) -> List[str]:
# if n == 1:
# return ['()']
# l = self.generateParenthesis(n-1)
# l_ = []
# for i in range(len(l)):
# if l[i]+'()' not in l_:
# l_.append(l[i]+'()')
# if '()'+l[i] not in l_:
# l_.append('()'+l[i])
# if '('+l[i]+')' not in l_:
# l_.append('('+l[i]+')')
# print(len(l_), len(["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]))
# return l_