From ed478339dfde11b432e87241135a4eef8911dce2 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 24 Sep 2024 14:13:27 +0800 Subject: [PATCH] 20240924 finished. --- src/problem/mod.rs | 3 +- ...mize_number_of_subsequences_in_a_string.rs | 43 +++++++++++++++++++ src/problem/p997_find_the_town_judge.rs | 4 +- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/problem/p2207_maximize_number_of_subsequences_in_a_string.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 29d29f4..d3d4a37 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -243,4 +243,5 @@ mod p2332_the_latest_time_to_catch_a_bus; mod p2414_length_of_the_longest_alphabetical_continuous_substring; mod p2376_count_special_integers; mod p2374_node_with_highest_edge_score; -mod p997_find_the_town_judge; \ No newline at end of file +mod p997_find_the_town_judge; +mod p2207_maximize_number_of_subsequences_in_a_string; \ No newline at end of file diff --git a/src/problem/p2207_maximize_number_of_subsequences_in_a_string.rs b/src/problem/p2207_maximize_number_of_subsequences_in_a_string.rs new file mode 100644 index 0000000..6fe274f --- /dev/null +++ b/src/problem/p2207_maximize_number_of_subsequences_in_a_string.rs @@ -0,0 +1,43 @@ +/** + * [2207] Maximize Number of Subsequences in a String + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn maximum_subsequence_count(text: String, pattern: String) -> i64 { + let pattern: Vec = pattern.chars().collect(); + let (first, second) = (pattern[0], pattern[1]); + + let (mut first_count, mut second_count) = (0, 0); + let mut result = 0; + + for c in text.chars() { + if c == second { + result += first_count; + second_count += 1; + } + + if c == first { + first_count += 1; + } + } + + result + first_count.max(second_count) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2207() { + assert_eq!(4, Solution::maximum_subsequence_count("abdcdbc".to_owned(), "ac".to_owned())); + assert_eq!(6, Solution::maximum_subsequence_count("aabb".to_owned(), "ab".to_owned())); + } +} diff --git a/src/problem/p997_find_the_town_judge.rs b/src/problem/p997_find_the_town_judge.rs index b4030b9..820559a 100644 --- a/src/problem/p997_find_the_town_judge.rs +++ b/src/problem/p997_find_the_town_judge.rs @@ -1,10 +1,8 @@ -use regex::escape; - /** * [997] Find the Town Judge */ pub struct Solution {} - + // submission codes start here