-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_parentheses.rs
61 lines (51 loc) · 1.53 KB
/
generate_parentheses.rs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// Given `n` pairs of parentheses, write a function to generate all combinations of well-formed
/// parentheses.
struct Solution;
impl Solution {
fn backtrack(results: &mut Vec<String>, stack: &mut Vec<char>, n: usize, open: usize, closed: usize) {
if open == closed && open == n {
results.push(stack.iter().collect());
} else {
if open < n {
stack.push('(');
Self::backtrack(results, stack, n, open + 1, closed);
stack.pop();
}
if closed < open {
stack.push(')');
Self::backtrack(results, stack, n, open, closed + 1);
stack.pop();
}
}
}
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut results = Vec::new();
let mut stack = Vec::new();
let n = n as usize;
Self::backtrack(&mut results, &mut stack, n, 0, 0);
results
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let n = 3;
let mut result = Solution::generate_parenthesis(n);
result.sort();
assert_eq!(result, vec!["((()))", "(()())", "(())()", "()(())", "()()()"]);
}
#[test]
fn example_2() {
let n = 1;
let result = Solution::generate_parenthesis(n);
assert_eq!(result, vec!["()"]);
}
#[test]
fn two() {
let n = 2;
let result = Solution::generate_parenthesis(n);
assert_eq!(result, vec!["(())", "()()"]);
}
}