Skip to content

Commit c68c157

Browse files
committed
Added Count Ways to Build Good Strings
1 parent 6c6b73d commit c68c157

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: src/count_ways_to_build_good_strings.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
5+
pub fn count_good_strings(low: i32, high: i32, zero: i32, one: i32) -> i32 {
6+
let low = low as usize;
7+
let high = high as usize;
8+
let zero = zero as usize;
9+
let one = one as usize;
10+
11+
let mut results = vec![0; high + 1];
12+
results[0] = 1;
13+
14+
for i in 1..=high {
15+
let mut value: i64 = 0;
16+
if i >= zero {
17+
value += results[i - zero] as i64;
18+
}
19+
if i >= one {
20+
value += results[i - one] as i64;
21+
}
22+
value = value % 1000000007;
23+
results[i] = value as i32;
24+
}
25+
let mut result: i64 = 0;
26+
for j in low..=high {
27+
result += results[j] as i64;
28+
result = result % 1000000007;
29+
}
30+
result as i32
31+
}
32+
33+
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use super::Solution;
38+
39+
#[test]
40+
fn example_1() {
41+
let low = 3;
42+
let high = 3;
43+
let zero = 1;
44+
let one = 1;
45+
let result = Solution::count_good_strings(low, high, zero, one);
46+
assert_eq!(result, 8);
47+
}
48+
49+
#[test]
50+
fn example_2() {
51+
let low = 2;
52+
let high = 3;
53+
let zero = 1;
54+
let one = 2;
55+
let result = Solution::count_good_strings(low, high, zero, one);
56+
assert_eq!(result, 5);
57+
}
58+
59+
}

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,8 @@ pub mod robot_to_print_the_lexicographically_smallest_string; // 2434 ✓
889889

890890
pub mod total_cost_to_hire_k_workers; // 2462 ✓
891891

892+
pub mod count_ways_to_build_good_strings; // 2466 ✓
893+
892894
pub mod circular_sentence; // 2490 ✓
893895

894896
pub mod longest_square_streak_in_an_array; // 2501 ✓

0 commit comments

Comments
 (0)