-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path22-generate-parentheses.py
More file actions
44 lines (35 loc) · 1.57 KB
/
22-generate-parentheses.py
File metadata and controls
44 lines (35 loc) · 1.57 KB
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
class Solution:
def generateParenthesis(self, n: int) -> list[str]:
result = []
def backtrack(current_string, open_count, close_count):
# Base case: If the current string length is equal to 2 * n,
# it means we have a complete combination.
if len(current_string) == n * 2:
result.append(current_string)
return
# Recursive case 1: Add an opening parenthesis if we still have open parentheses to add.
# The number of open parentheses must not exceed n.
if open_count < n:
backtrack(current_string + "(", open_count + 1, close_count)
# Recursive case 2: Add a closing parenthesis if the number of closing parentheses
# is less than the number of open parentheses. This ensures well-formedness.
if close_count < open_count:
backtrack(current_string + ")", open_count, close_count + 1)
backtrack("", 0, 0)
return result
# Test cases
if __name__ == "__main__":
sol = Solution()
print("Testing Generate Parentheses:")
# Test 1
n1 = 3
result1 = sol.generateParenthesis(n1)
print(f"Input: n = {n1} -> Output: {result1}") # Expected: ['((()))', '(()())', '(())()', '()(())', '()()()']
# Test 2
n2 = 1
result2 = sol.generateParenthesis(n2)
print(f"Input: n = {n2} -> Output: {result2}") # Expected: ['()']
# Test 3
n3 = 2
result3 = sol.generateParenthesis(n3)
print(f"Input: n = {n3} -> Output: {result3}") # Expected: ['(())', '()()']