-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
44 lines (37 loc) · 992 Bytes
/
mod.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
pub struct Solution;
use std::collections::HashSet;
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
if n == 1 {
return vec!["()".to_string()];
}
Solution::generate_parenthesis(n - 1)
.into_iter()
.flat_map(|str| {
let mut result = vec![];
for i in 0..str.len() {
result.push(format!("{}(){}", &str[0..i], &str[i..]));
}
result
})
.collect::<HashSet<_>>()
.into_iter()
.collect()
}
}
#[cfg(test)]
mod tests {
use crate::common::ToSortVec;
use super::*;
#[test]
fn test1() {
assert_eq!(
Solution::generate_parenthesis(3).to_sort_vec(),
["((()))", "(()())", "(())()", "()(())", "()()()"].to_sort_vec()
);
}
#[test]
fn test2() {
assert_eq!(Solution::generate_parenthesis(1), ["()"]);
}
}