Skip to content

Commit

Permalink
#19: add solution for exercise #22 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielvictorcf authored and gosiqueira committed Nov 1, 2022
1 parent 61460be commit 87092ab
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions rust/backtracking/0022-generate-parentheses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
fn gen(s: String, open: i32, closed: i32) -> Vec<String> {
let mut answers = vec![];
if open == 0 && closed == 0 {
return vec![s];
}

if open > 0 {
answers.append(&mut gen(s.clone()+"(", open-1, closed+1));
}

if closed > 0 {
answers.append(&mut gen(s.clone()+")", open, closed-1));
}

answers
}

gen("".to_string(), n, 0)
}
}

0 comments on commit 87092ab

Please sign in to comment.