From fa4dedaf4614482a4cfee7ba6ec989f25f247831 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 19 Sep 2024 13:38:03 +0800 Subject: [PATCH] 20240919 finished. --- src/problem/mod.rs | 3 +- ...ngest_alphabetical_continuous_substring.rs | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2414_length_of_the_longest_alphabetical_continuous_substring.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9f4adc8..a6aec31 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -239,4 +239,5 @@ mod p2390_removing_stars_from_a_string; mod p2848_points_that_intersect_with_cars; mod p1184_distance_between_bus_stops; mod p815_bus_routes; -mod p2332_the_latest_time_to_catch_a_bus; \ No newline at end of file +mod p2332_the_latest_time_to_catch_a_bus; +mod p2414_length_of_the_longest_alphabetical_continuous_substring; \ No newline at end of file diff --git a/src/problem/p2414_length_of_the_longest_alphabetical_continuous_substring.rs b/src/problem/p2414_length_of_the_longest_alphabetical_continuous_substring.rs new file mode 100644 index 0000000..627a8ab --- /dev/null +++ b/src/problem/p2414_length_of_the_longest_alphabetical_continuous_substring.rs @@ -0,0 +1,42 @@ +/** + * [2414] Length of the Longest Alphabetical Continuous Substring + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn longest_continuous_substring(s: String) -> i32 { + let s: Vec = s.chars().map(|c| c.into()).collect(); + + let mut result = 1; + let mut length = 1; + let mut last_char = s[0]; + + for i in 1..s.len() { + if last_char + 1 == s[i] { + length += 1; + result = result.max(length); + } else { + length = 1; + } + last_char = s[i]; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2414() { + assert_eq!(2, Solution::longest_continuous_substring("abacaba".to_string())); + assert_eq!(5, Solution::longest_continuous_substring("abcde".to_string())); + } +}