From f0ba102a70bebd3a87b88644d0d254853d006e0a Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 29 Oct 2024 11:11:33 +0800 Subject: [PATCH] 20241029 finished. --- src/problem/mod.rs | 2 + ...e_binary_strings_without_adjacent_zeros.rs | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/problem/p3211_generate_binary_strings_without_adjacent_zeros.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4a16365..1c2568a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -282,3 +282,5 @@ mod p3181_maximum_total_reward_using_operations_ii; mod p684_redundant_connection; mod p685_redundant_connection_ii; + +mod p3211_generate_binary_strings_without_adjacent_zeros; diff --git a/src/problem/p3211_generate_binary_strings_without_adjacent_zeros.rs b/src/problem/p3211_generate_binary_strings_without_adjacent_zeros.rs new file mode 100644 index 0000000..00862f5 --- /dev/null +++ b/src/problem/p3211_generate_binary_strings_without_adjacent_zeros.rs @@ -0,0 +1,49 @@ +/** + * [3211] Generate Binary Strings Without Adjacent Zeros + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn valid_strings(n: i32) -> Vec { + let n = n as usize; + let mut result = Vec::new(); + let mut str = vec!['0'; n]; + + Self::dfs(0, false, &mut str, &mut result); + + result + } + + fn dfs(i: usize, last_zero: bool, str: &mut Vec, result: &mut Vec) { + if i >= str.len() { + result.push(str.iter().collect()); + return; + } + + if !last_zero { + str[i] = '0'; + Self::dfs(i + 1, true, str, result); + } + + str[i] = '1'; + Self::dfs(i + 1, false, str, result); + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3211() { + assert_eq!( + vec_string!("010", "011", "101", "110", "111"), + Solution::valid_strings(3) + ); + assert_eq!(vec_string!("0", "1"), Solution::valid_strings(1)); + } +}