20240919 finished.

This commit is contained in:
jackfiled 2024-09-19 13:38:03 +08:00
parent 0a79140c71
commit fa4dedaf46
2 changed files with 44 additions and 1 deletions

View File

@ -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;
mod p2332_the_latest_time_to_catch_a_bus;
mod p2414_length_of_the_longest_alphabetical_continuous_substring;

View File

@ -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<u32> = 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()));
}
}