Skip to content

Commit

Permalink
Add problem 1839: Longest Substring Of All Vowels in Order
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 14, 2023
1 parent f3ea4e5 commit 334fe10
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
28 changes: 28 additions & 0 deletions src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}

0 comments on commit 334fe10

Please sign in to comment.