-
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 1639: Number of Ways to Form a Target String Given a Dict…
…ionary
- Loading branch information
Showing
4 changed files
with
124 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
52 changes: 52 additions & 0 deletions
52
...lem_1639_number_of_ways_to_form_a_target_string_given_a_dictionary/dynamic_programming.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,52 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn num_ways(words: Vec<String>, target: String) -> i32 { | ||
// Count characters in each column. | ||
|
||
let word_length = words.first().map_or(0, String::len); | ||
let mut columns = vec![[0_u16; 26]; word_length].into_boxed_slice(); | ||
|
||
for word in words { | ||
for (counts, c) in columns.iter_mut().zip(word.bytes()) { | ||
counts[usize::from(c) - usize::from(b'a')] += 1; | ||
} | ||
} | ||
|
||
// Dynamic programming. | ||
|
||
let mut cache = vec![0_u32; target.len()].into_boxed_slice(); | ||
|
||
for counts in &*columns { | ||
let mut top_left = 1; | ||
|
||
for (target, c) in cache.iter_mut().zip(target.bytes()) { | ||
let drop = u64::from(*target); | ||
let pick = u64::from(top_left) * u64::from(counts[usize::from(c) - usize::from(b'a')]); | ||
|
||
top_left = *target; | ||
*target = ((drop + pick) % 1_000_000_007) as _; | ||
} | ||
} | ||
|
||
*cache.last().unwrap() as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn num_ways(words: Vec<String>, target: String) -> i32 { | ||
Self::num_ways(words, target) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...m_1639_number_of_ways_to_form_a_target_string_given_a_dictionary/dynamic_programming_2.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 ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn num_ways(words: Vec<String>, target: String) -> i32 { | ||
let word_length = words.first().map_or(0, String::len); | ||
let mut cache = vec![0_u32; target.len()].into_boxed_slice(); | ||
|
||
for i in 0..word_length { | ||
let mut counts = [0_u16; 26]; | ||
|
||
for word in &words { | ||
counts[usize::from(word.as_bytes()[i]) - usize::from(b'a')] += 1; | ||
} | ||
|
||
let mut top_left = 1; | ||
|
||
for (target, c) in cache.iter_mut().zip(target.bytes()) { | ||
let drop = u64::from(*target); | ||
let pick = u64::from(top_left) * u64::from(counts[usize::from(c) - usize::from(b'a')]); | ||
|
||
top_left = *target; | ||
*target = ((drop + pick) % 1_000_000_007) as _; | ||
} | ||
} | ||
|
||
*cache.last().unwrap() as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn num_ways(words: Vec<String>, target: String) -> i32 { | ||
Self::num_ways(words, target) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/problem_1639_number_of_ways_to_form_a_target_string_given_a_dictionary/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,25 @@ | ||
pub mod dynamic_programming; | ||
pub mod dynamic_programming_2; | ||
|
||
pub trait Solution { | ||
fn num_ways(words: Vec<String>, target: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&["acca", "bbbb", "caca"] as &[_], "aba"), 6), | ||
((&["abba", "baab"], "bab"), 4), | ||
]; | ||
|
||
for ((words, target), expected) in test_cases { | ||
assert_eq!( | ||
S::num_ways(words.iter().copied().map(str::to_string).collect(), target.to_string()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |