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