-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2138: Divide a String Into Groups of Size k
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/problem_2138_divide_a_string_into_groups_of_size_k/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::iter; | ||
|
||
impl Solution { | ||
pub fn divide_string(s: String, k: i32, fill: char) -> Vec<String> { | ||
let n = s.len(); | ||
let k = k as u32 as usize; | ||
let mut prev = 0; | ||
let mut result = Vec::with_capacity(s.len().div_ceil(k)); | ||
|
||
while let Some(s) = s.get(prev..prev + k) { | ||
result.push(s.to_string()); | ||
|
||
prev += k; | ||
} | ||
|
||
if n != prev { | ||
let mut group = s[prev..].to_string(); | ||
|
||
group.extend(iter::repeat(fill).take(k - (n - prev))); | ||
|
||
result.push(group); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn divide_string(s: String, k: i32, fill: char) -> Vec<String> { | ||
Self::divide_string(s, k, fill) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/problem_2138_divide_a_string_into_groups_of_size_k/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn divide_string(s: String, k: i32, fill: char) -> Vec<String>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("abcdefghi", 3, 'x'), &["abc", "def", "ghi"] as &[_]), | ||
(("abcdefghij", 3, 'x'), &["abc", "def", "ghi", "jxx"]), | ||
]; | ||
|
||
for ((s, k, fill), expected) in test_cases { | ||
assert_eq!(S::divide_string(s.to_string(), k, fill), expected); | ||
} | ||
} | ||
} |