forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateParentheses.swift
33 lines (27 loc) · 1019 Bytes
/
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
/**
* Question Link: https://leetcode.com/problems/generate-parentheses/
* Primary idea: Dynamic Programming, generate the current parentheses based on previous one
* Note: Use a set to avoid duplicates
* Time Complexity: O(n^2), Space Complexity: O(n)
*
*/
class GenerateParentheses {
func generateParenthesis(n: Int) -> [String] {
var set = Set<String>()
guard n > 0 else {
return [""]
}
for str in generateParenthesis(n - 1) {
for (i, char) in str.characters.enumerate() {
if char == "(" {
set.insert(_substring(str, 0, i + 1) + "()" + _substring(str, i + 1, str.characters.count))
}
}
set.insert(str + "()")
}
return [String](set)
}
private func _substring(str: String, _ start: Int, _ end: Int) -> String {
return str[str.startIndex.advancedBy(start) ..< str.startIndex.advancedBy(end)]
}
}