-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1839: Longest Substring Of All Vowels in Order
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/problem_1839_longest_substring_of_all_vowels_in_order/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/problem_1839_longest_substring_of_all_vowels_in_order/state_machine.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |