Skip to content

Commit 1859805

Browse files
committed
Add problem 2269: Find the K-Beauty of a Number
1 parent 40d5e7e commit 1859805

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ pub mod problem_2262_total_appeal_of_a_string;
15871587
pub mod problem_2264_largest_3_same_digit_number_in_string;
15881588
pub mod problem_2265_count_nodes_equal_to_average_of_subtree;
15891589
pub mod problem_2266_count_number_of_texts;
1590+
pub mod problem_2269_find_the_k_beauty_of_a_number;
15901591
pub mod problem_2270_number_of_ways_to_split_array;
15911592

15921593
#[cfg(test)]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::num::NonZeroU32;
6+
7+
impl Solution {
8+
pub fn divisor_substrings(num: i32, k: i32) -> i32 {
9+
let num = num as u32;
10+
let mut k = k as u32;
11+
let mut x = num;
12+
let mut base = 1;
13+
let mut window = 0;
14+
15+
loop {
16+
window += base * (x % 10);
17+
x /= 10;
18+
k -= 1;
19+
20+
if k == 0 {
21+
break;
22+
}
23+
24+
base *= 10;
25+
}
26+
27+
let mut result = 0;
28+
29+
loop {
30+
if NonZeroU32::new(window).map_or(false, |window| num % window == 0) {
31+
result += 1;
32+
}
33+
34+
if x == 0 {
35+
break;
36+
}
37+
38+
window = window / 10 + base * (x % 10);
39+
x /= 10;
40+
}
41+
42+
result
43+
}
44+
}
45+
46+
// ------------------------------------------------------ snip ------------------------------------------------------ //
47+
48+
impl super::Solution for Solution {
49+
fn divisor_substrings(num: i32, k: i32) -> i32 {
50+
Self::divisor_substrings(num, k)
51+
}
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
#[test]
57+
fn test_solution() {
58+
super::super::tests::run::<super::Solution>();
59+
}
60+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn divisor_substrings(num: i32, k: i32) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [((240, 2), 2), ((430_043, 2), 2)];
13+
14+
for ((num, k), expected) in test_cases {
15+
assert_eq!(S::divisor_substrings(num, k), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)