### 题目

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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
``````

### 思路

what constraint? 给定m个(和n 个), 如果m > n 则是不可能的，如果相同，则只能放( , 如果m < n 则放(和) 都是可行的。

### Swift 题解

``````func generateParenthesis(_ n: Int) -> [String] {
var result = [String]()

helper(current: (n,n), result: &result, currentString: "")

return result
}

func helper(current: (Int, Int), result: inout [String], currentString: String) {
let left = current.0
let right = current.1
if left == 0 && right == 0 {
result.append(currentString)

return
}

if left > right {
return
} else if left == right {
helper(current: (left - 1, right), result: &result, currentString: currentString + "(")
} else {
//left < right
if left > 0 {
helper(current: (left - 1, right), result: &result, currentString: currentString + "(")
}
helper(current: (left, right - 1), result: &result, currentString: currentString + ")")
}
}
``````