diff --git a/src/lib.rs b/src/lib.rs index d66278c8..5c5b17fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1587,6 +1587,7 @@ pub mod problem_2262_total_appeal_of_a_string; pub mod problem_2264_largest_3_same_digit_number_in_string; pub mod problem_2265_count_nodes_equal_to_average_of_subtree; pub mod problem_2266_count_number_of_texts; +pub mod problem_2269_find_the_k_beauty_of_a_number; pub mod problem_2270_number_of_ways_to_split_array; #[cfg(test)] diff --git a/src/problem_2269_find_the_k_beauty_of_a_number/iterative.rs b/src/problem_2269_find_the_k_beauty_of_a_number/iterative.rs new file mode 100644 index 00000000..55ba06c0 --- /dev/null +++ b/src/problem_2269_find_the_k_beauty_of_a_number/iterative.rs @@ -0,0 +1,60 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::num::NonZeroU32; + +impl Solution { + pub fn divisor_substrings(num: i32, k: i32) -> i32 { + let num = num as u32; + let mut k = k as u32; + let mut x = num; + let mut base = 1; + let mut window = 0; + + loop { + window += base * (x % 10); + x /= 10; + k -= 1; + + if k == 0 { + break; + } + + base *= 10; + } + + let mut result = 0; + + loop { + if NonZeroU32::new(window).map_or(false, |window| num % window == 0) { + result += 1; + } + + if x == 0 { + break; + } + + window = window / 10 + base * (x % 10); + x /= 10; + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn divisor_substrings(num: i32, k: i32) -> i32 { + Self::divisor_substrings(num, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2269_find_the_k_beauty_of_a_number/mod.rs b/src/problem_2269_find_the_k_beauty_of_a_number/mod.rs new file mode 100644 index 00000000..aa896ce2 --- /dev/null +++ b/src/problem_2269_find_the_k_beauty_of_a_number/mod.rs @@ -0,0 +1,18 @@ +pub mod iterative; + +pub trait Solution { + fn divisor_substrings(num: i32, k: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [((240, 2), 2), ((430_043, 2), 2)]; + + for ((num, k), expected) in test_cases { + assert_eq!(S::divisor_substrings(num, k), expected); + } + } +}