-
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 2023: Number of Pairs of Strings With Concatenation Equal…
… to Target
- Loading branch information
Showing
3 changed files
with
79 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
src/problem_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target/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,52 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn num_of_pairs(nums: Vec<String>, target: String) -> i32 { | ||
let target = target.as_bytes(); | ||
let n = target.len(); | ||
let mut prefix_counts = vec![0_u8; n + 1].into_boxed_slice(); | ||
|
||
for num in &nums { | ||
if target.starts_with(num.as_bytes()) { | ||
prefix_counts[num.len()] += 1; | ||
} | ||
} | ||
|
||
let mut result = 0; | ||
|
||
for num in &nums { | ||
if target.ends_with(num.as_bytes()) { | ||
result += u16::from(prefix_counts[n - num.len()]); | ||
} | ||
} | ||
|
||
if n % 2 == 0 { | ||
let half = n / 2; | ||
let (left, right) = target.split_at(half); | ||
|
||
if left == right { | ||
result -= u16::from(prefix_counts[half]); | ||
} | ||
} | ||
|
||
i32::from(result) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn num_of_pairs(nums: Vec<String>, target: String) -> i32 { | ||
Self::num_of_pairs(nums, target) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/problem_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target/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,26 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn num_of_pairs(nums: Vec<String>, target: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&["777", "7", "77", "77"] as &[_], "7777"), 4), | ||
((&["123", "4", "12", "34"], "1234"), 2), | ||
((&["1", "1", "1"], "11"), 6), | ||
((&["74", "1", "67", "1", "74"], "174"), 4), | ||
]; | ||
|
||
for ((nums, target), expected) in test_cases { | ||
assert_eq!( | ||
S::num_of_pairs(nums.iter().copied().map(str::to_string).collect(), target.to_string()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |