-
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 2207: Maximize Number of Subsequences in a String
- Loading branch information
Showing
3 changed files
with
58 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
36 changes: 36 additions & 0 deletions
36
src/problem_2207_maximize_number_of_subsequences_in_a_string/greedy.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,36 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn maximum_subsequence_count(text: String, pattern: String) -> i64 { | ||
let [left, right]: [_; 2] = pattern.into_bytes().try_into().ok().unwrap(); | ||
let mut left_count = 0_u64; | ||
let mut right_count = 0_u64; | ||
let mut result = 0; | ||
|
||
for c in text.into_bytes() { | ||
result += left_count * u64::from(c == right); | ||
left_count += u64::from(c == left); | ||
right_count += u64::from(c == right); | ||
} | ||
|
||
(result + left_count.max(right_count)) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn maximum_subsequence_count(text: String, pattern: String) -> i64 { | ||
Self::maximum_subsequence_count(text, pattern) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/problem_2207_maximize_number_of_subsequences_in_a_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,21 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn maximum_subsequence_count(text: String, pattern: String) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(("abdcdbc", "ac"), 4), (("aabb", "ab"), 6)]; | ||
|
||
for ((text, pattern), expected) in test_cases { | ||
assert_eq!( | ||
S::maximum_subsequence_count(text.to_string(), pattern.to_string()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |