-
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 2287: Rearrange Characters to Make Target String
- Loading branch information
Showing
3 changed files
with
70 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
47 changes: 47 additions & 0 deletions
47
src/problem_2287_rearrange_characters_to_make_target_string/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,47 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroU8; | ||
|
||
impl Solution { | ||
pub fn rearrange_characters(s: String, target: String) -> i32 { | ||
let mut s_counts = [0_u8; 26]; | ||
|
||
for c in s.into_bytes() { | ||
s_counts[usize::from(c) - usize::from(b'a')] += 1; | ||
} | ||
|
||
let mut target_counts = [0_u8; 26]; | ||
|
||
for c in target.into_bytes() { | ||
target_counts[usize::from(c) - usize::from(b'a')] += 1; | ||
} | ||
|
||
let mut result = u8::MAX; | ||
|
||
for (&c_count, &target_count) in s_counts.iter().zip(&target_counts) { | ||
if let Some(target_count) = NonZeroU8::new(target_count) { | ||
result = result.min(c_count / target_count); | ||
} | ||
} | ||
|
||
i32::from(result) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn rearrange_characters(s: String, target: String) -> i32 { | ||
Self::rearrange_characters(s, target) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_2287_rearrange_characters_to_make_target_string/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,22 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn rearrange_characters(s: String, target: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("ilovecodingonleetcode", "code"), 2), | ||
(("abcba", "abc"), 1), | ||
(("abbaccaddaeea", "aaaaa"), 1), | ||
]; | ||
|
||
for ((s, target), expected) in test_cases { | ||
assert_eq!(S::rearrange_characters(s.to_string(), target.to_string()), expected); | ||
} | ||
} | ||
} |