|
| 1 | +/// Generates all combinations of well-formed parentheses given a non-negative integer `n`. |
| 2 | +/// |
| 3 | +/// This function uses backtracking to generate all possible combinations of well-formed |
| 4 | +/// parentheses. The resulting combinations are returned as a vector of strings. |
| 5 | +/// |
| 6 | +/// # Arguments |
| 7 | +/// |
| 8 | +/// * `n` - A non-negative integer representing the number of pairs of parentheses. |
| 9 | +pub fn generate_parentheses(n: usize) -> Vec<String> { |
| 10 | + let mut result = Vec::new(); |
| 11 | + if n > 0 { |
| 12 | + generate("", 0, 0, n, &mut result); |
| 13 | + } |
| 14 | + result |
| 15 | +} |
| 16 | + |
| 17 | +/// Helper function for generating parentheses recursively. |
| 18 | +/// |
| 19 | +/// This function is called recursively to build combinations of well-formed parentheses. |
| 20 | +/// It tracks the number of open and close parentheses added so far and adds a new parenthesis |
| 21 | +/// if it's valid to do so. |
| 22 | +/// |
| 23 | +/// # Arguments |
| 24 | +/// |
| 25 | +/// * `current` - The current string of parentheses being built. |
| 26 | +/// * `open_count` - The count of open parentheses in the current string. |
| 27 | +/// * `close_count` - The count of close parentheses in the current string. |
| 28 | +/// * `n` - The total number of pairs of parentheses to be generated. |
| 29 | +/// * `result` - A mutable reference to the vector storing the generated combinations. |
| 30 | +fn generate( |
| 31 | + current: &str, |
| 32 | + open_count: usize, |
| 33 | + close_count: usize, |
| 34 | + n: usize, |
| 35 | + result: &mut Vec<String>, |
| 36 | +) { |
| 37 | + if current.len() == (n * 2) { |
| 38 | + result.push(current.to_string()); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if open_count < n { |
| 43 | + let new_str = current.to_string() + "("; |
| 44 | + generate(&new_str, open_count + 1, close_count, n, result); |
| 45 | + } |
| 46 | + |
| 47 | + if close_count < open_count { |
| 48 | + let new_str = current.to_string() + ")"; |
| 49 | + generate(&new_str, open_count, close_count + 1, n, result); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::*; |
| 56 | + |
| 57 | + macro_rules! generate_parentheses_tests { |
| 58 | + ($($name:ident: $test_case:expr,)*) => { |
| 59 | + $( |
| 60 | + #[test] |
| 61 | + fn $name() { |
| 62 | + let (n, expected_result) = $test_case; |
| 63 | + assert_eq!(generate_parentheses(n), expected_result); |
| 64 | + } |
| 65 | + )* |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + generate_parentheses_tests! { |
| 70 | + test_generate_parentheses_0: (0, Vec::<String>::new()), |
| 71 | + test_generate_parentheses_1: (1, vec!["()"]), |
| 72 | + test_generate_parentheses_2: (2, vec!["(())", "()()"]), |
| 73 | + test_generate_parentheses_3: (3, vec!["((()))", "(()())", "(())()", "()(())", "()()()"]), |
| 74 | + test_generate_parentheses_4: (4, vec!["(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"]), |
| 75 | + } |
| 76 | +} |
0 commit comments