20240924 finished.
This commit is contained in:
parent
b513afc8d2
commit
ed478339df
|
@ -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;
|
||||
mod p997_find_the_town_judge;
|
||||
mod p2207_maximize_number_of_subsequences_in_a_string;
|
|
@ -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<char> = 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()));
|
||||
}
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
use regex::escape;
|
||||
|
||||
/**
|
||||
* [997] Find the Town Judge
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user