-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathContents.swift
38 lines (31 loc) · 873 Bytes
/
Contents.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
37
38
import Foundation
/*
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
*/
func generateParenthesis(_ n: Int) -> [String] {
var result = [String]()
parenthesesHelper(n: n, openLeft: 0, stream: "", result: &result)
return result
}
func parenthesesHelper(n: Int, openLeft: Int, stream: String, result: inout [String]) {
if n != 0 {
parenthesesHelper(n: n - 1, openLeft: openLeft + 1, stream: stream + "(", result: &result)
}
if openLeft > 0 {
let newS = stream + ")"
parenthesesHelper(n: n, openLeft: openLeft - 1, stream: newS, result: &result)
}
if n == 0 && openLeft == 0 {
result.append(stream)
}
return
}
generateParenthesis(2)