forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateParentheses.swift
36 lines (30 loc) · 1.07 KB
/
GenerateParentheses.swift
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
/**
* Question Link: https://leetcode.com/problems/generate-parentheses/
* Primary idea: Insert left and right parentheses and ensure they are valid
* Time Complexity: O(2^n), Space Complexity: O(n)
*
*/
class GenerateParentheses {
func generateParenthesis(_ n: Int) -> [String] {
var paths = [String](), path = [Character](repeating: " ", count: 2 * n)
helper(&paths, &path, n, n, 0)
return paths
}
func helper(_ paths: inout [String], _ path: inout [Character], _ leftCount: Int, _ rightCount: Int, _ index: Int) {
if leftCount < 0 || leftCount > rightCount {
return
}
if leftCount == 0 && rightCount == 0 {
paths.append(String(path))
return
}
if leftCount > 0 {
path[index] = "("
helper(&paths, &path, leftCount - 1, rightCount, index + 1)
}
if rightCount > leftCount {
path[index] = ")"
helper(&paths, &path, leftCount, rightCount - 1, index + 1)
}
}
}