Medium
Given n
pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: [”((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
Example 2:
Input: n = 1
Output: [”()”]
Constraints:
1 <= n <= 8
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut result = Vec::new();
let mut current = String::new();
Solution::generate(&mut current, &mut result, n, n);
result
}
fn generate(current: &mut String, result: &mut Vec<String>, open: i32, close: i32) {
if open == 0 && close == 0 {
result.push(current.clone());
return;
}
if open > 0 {
current.push('(');
Solution::generate(current, result, open - 1, close);
current.pop();
}
if close > 0 && open < close {
current.push(')');
Solution::generate(current, result, open, close - 1);
current.pop();
}
}
}