From 334fe1043e0a4439c25a2f6bc3b03b2d297d6baa Mon Sep 17 00:00:00 2001 From: EFanZh Date: Thu, 14 Sep 2023 23:50:44 +0800 Subject: [PATCH] Add problem 1839: Longest Substring Of All Vowels in Order --- src/lib.rs | 1 + .../mod.rs | 28 +++++ .../state_machine.rs | 118 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs create mode 100644 src/problem_1839_longest_substring_of_all_vowels_in_order/state_machine.rs diff --git a/src/lib.rs b/src/lib.rs index 9816d6c9..78ff1858 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1415,6 +1415,7 @@ pub mod problem_1827_minimum_operations_to_make_the_array_increasing; pub mod problem_1832_check_if_the_sentence_is_pangram; pub mod problem_1833_maximum_ice_cream_bars; pub mod problem_1837_sum_of_digits_in_base_k; +pub mod problem_1839_longest_substring_of_all_vowels_in_order; #[cfg(test)] mod test_utilities; diff --git a/src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs b/src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs new file mode 100644 index 00000000..bc64e7a3 --- /dev/null +++ b/src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs @@ -0,0 +1,28 @@ +pub mod state_machine; + +pub trait Solution { + fn longest_beautiful_substring(word: String) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ("aeiaaioaaaaeiiiiouuuooaauuaeiu", 13), + ("aeeeiiiioooauuuaeiou", 5), + ("a", 0), + ("iuaoeieaeeaeeiouueae", 7), + ("eauoiouieaaoueiuaieoeauoiaueoiaeoiuieuaoiaeouiaueo", 0), + ("aeuaeiiouoeiaeiiiii", 6), + ("aeio", 0), + ("aeioe", 0), + ("aeioua", 5), + ]; + + for (word, expected) in test_cases { + assert_eq!(S::longest_beautiful_substring(word.to_string()), expected); + } + } +} diff --git a/src/problem_1839_longest_substring_of_all_vowels_in_order/state_machine.rs b/src/problem_1839_longest_substring_of_all_vowels_in_order/state_machine.rs new file mode 100644 index 00000000..a28c7785 --- /dev/null +++ b/src/problem_1839_longest_substring_of_all_vowels_in_order/state_machine.rs @@ -0,0 +1,118 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn longest_beautiful_substring(word: String) -> i32 { + let mut result = 0; + let mut iter = word.bytes(); + + 'outer: while let Some(c) = iter.next() { + if c != b'a' { + continue; + } + + // Start. + + 'start: loop { + let mut length = 1; + + // a. + + loop { + match iter.next() { + None => break 'outer, + Some(b'a') => length += 1, + Some(b'e') => break, + Some(_) => continue 'outer, + } + } + + length += 1; + + // e. + + loop { + match iter.next() { + None => break 'outer, + Some(b'a') => continue 'start, + Some(b'e') => length += 1, + Some(b'i') => break, + Some(_) => continue 'outer, + } + } + + length += 1; + + // i. + + loop { + match iter.next() { + None => break 'outer, + Some(b'a') => continue 'start, + Some(b'i') => length += 1, + Some(b'o') => break, + Some(_) => continue 'outer, + } + } + + length += 1; + + // o. + + loop { + match iter.next() { + None => break 'outer, + Some(b'a') => continue 'start, + Some(b'o') => length += 1, + Some(b'u') => break, + Some(_) => continue 'outer, + } + } + + length += 1; + + // u. + + loop { + match iter.next() { + None => { + result = result.max(length); + + break 'outer; + } + Some(b'a') => { + result = result.max(length); + + continue 'start; + } + Some(b'u') => length += 1, + Some(_) => { + result = result.max(length); + + continue 'outer; + } + } + } + } + } + + result + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn longest_beautiful_substring(word: String) -> i32 { + Self::longest_beautiful_substring(word) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}