Skip to content

Commit 7fea636

Browse files
committed
feat: easy: divide_a_string_into_groups_of_size_k.rs
1 parent 3d44fa5 commit 7fea636

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn divide_string(s: String, k: i32, fill: char) -> Vec<String> {
5+
let mut out = vec![];
6+
let k = k as usize;
7+
8+
let mut i = 0;
9+
while i < s.len() {
10+
let mut chunk = s[i..s.len().min(i + k)].to_string();
11+
while chunk.len() < k {
12+
chunk.push(fill);
13+
}
14+
out.push(chunk);
15+
i += k;
16+
}
17+
18+
out
19+
}
20+
}
21+
22+
#[test]
23+
fn test() {
24+
let test_cases = [
25+
("abcdefghi", 3, 'x', vec!["abc", "def", "ghi"]),
26+
("abcdefghij", 3, 'x', vec!["abc", "def", "ghi", "jxx"]),
27+
];
28+
29+
for (s, k, fill, expected) in test_cases {
30+
assert_eq!(
31+
Solution::divide_string(s.to_string(), k, fill),
32+
expected,
33+
"failed for {s}, k {k}, fill {fill}",
34+
);
35+
}
36+
}

src/easy/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod check_if_array_is_sorted_and_rotated;
33
mod check_if_one_string_swap_can_make_strings_equal;
44
mod clear_digits;
55
mod count_pairs_whose_sum_is_less_than_target;
6+
mod divide_a_string_into_groups_of_size_k;
67
mod find_the_difference;
78
mod find_words_containing_character;
89
mod guess_number_higher_or_lower;

0 commit comments

Comments
 (0)