Skip to content

Commit fe39106

Browse files
committed
Add problem 3211: Generate Binary Strings Without Adjacent Zeros
1 parent d6101b3 commit fe39106

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,7 @@ pub mod problem_3201_find_the_maximum_length_of_valid_subsequence_i;
21382138
pub mod problem_3206_alternating_groups_i;
21392139
pub mod problem_3208_alternating_groups_ii;
21402140
pub mod problem_3210_find_the_encrypted_string;
2141+
pub mod problem_3211_generate_binary_strings_without_adjacent_zeros;
21412142
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21422143

21432144
#[cfg(test)]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
struct Context {
6+
buffer: String,
7+
result: Vec<String>,
8+
length: usize,
9+
}
10+
11+
impl Context {
12+
fn emit(&mut self) {
13+
self.result.push(self.buffer.clone());
14+
}
15+
16+
fn push<const C: char>(&mut self, f: impl FnOnce(&mut Self)) {
17+
self.buffer.push(C);
18+
self.length -= 1;
19+
20+
f(self);
21+
22+
self.length += 1;
23+
self.buffer.pop();
24+
}
25+
}
26+
27+
impl Solution {
28+
fn dfs_0(context: &mut Context) {
29+
if context.length == 0 {
30+
context.emit();
31+
} else {
32+
context.push::<'1'>(Self::dfs_1);
33+
}
34+
}
35+
36+
fn dfs_1(context: &mut Context) {
37+
if context.length == 0 {
38+
context.emit();
39+
} else {
40+
context.push::<'0'>(Self::dfs_0);
41+
context.push::<'1'>(Self::dfs_1);
42+
}
43+
}
44+
45+
pub fn valid_strings(n: i32) -> Vec<String> {
46+
let n = n.cast_unsigned() as usize;
47+
48+
let mut context = Context {
49+
buffer: String::with_capacity(n),
50+
result: Vec::new(),
51+
length: n,
52+
};
53+
54+
Self::dfs_1(&mut context);
55+
56+
context.result
57+
}
58+
}
59+
60+
// ------------------------------------------------------ snip ------------------------------------------------------ //
61+
62+
impl super::Solution for Solution {
63+
fn valid_strings(n: i32) -> Vec<String> {
64+
Self::valid_strings(n)
65+
}
66+
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
#[test]
71+
fn test_solution() {
72+
super::super::tests::run::<super::Solution>();
73+
}
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod dfs;
2+
3+
pub trait Solution {
4+
fn valid_strings(n: i32) -> Vec<String>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [(3, &["010", "011", "101", "110", "111"] as &[_]), (1, &["0", "1"])];
14+
15+
for (n, expected) in test_cases {
16+
assert_eq!(test_utilities::unstable_sorted(S::valid_strings(n)), expected);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)