Skip to content

Commit

Permalink
[DP] Update solution to Generate Parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Mar 28, 2018
1 parent 5818b80 commit cb391c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions DP/GenerateParentheses.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
/**
* 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)
* 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 set = Set<String>()
var paths = [String](), path = [Character](repeating: " ", count: 2 * n)

guard n > 0 else {
return [""]
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
}

for str in generateParenthesis(n - 1) {
for (i, char) in str.characters.enumerated() {
if char == "(" {
set.insert(String(Array(str.characters)[0...i]) + "()" + String(Array(str.characters)[i + 1..<str.characters.count]))
}
}
set.insert(str + "()")
if leftCount == 0 && rightCount == 0 {
paths.append(String(path))
return
}

return [String](set)
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)
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)| [Swift](./DP/UniquePathsII.swift)| Medium| O(mn)| O(mn)|
[Decode Ways](https://leetcode.com/problems/decode-ways/)| [Swift](./DP/DecodeWays.swift) | O(n)|O(n)|
[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [Swift](./DP/MinimumPathSum.swift)| Medium| O(mn)| O(mn)|
[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Swift](./DP/GenerateParentheses.swift)| Medium| O(n^2)| O(n)|
[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [Swift](./DP/GenerateParentheses.swift)| Medium| O(2^n)| O(n)|
[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)| [Swift](./DP/DifferentWaysAddParentheses.swift)| Medium| O(n^n)| O(n)|
[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Swift](./DP/BestTimeBuySellStock.swift)| Easy| O(n)| O(1)|
[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)| [Swift](./DP/BestTimeBuySellStockII.swift)| Medium| O(n)| O(1)|
Expand Down

0 comments on commit cb391c9

Please sign in to comment.