Skip to content

Commit

Permalink
finish longest substring of all vowels in order
Browse files Browse the repository at this point in the history
  • Loading branch information
Checky-Hu committed Apr 28, 2021
1 parent c3cf24e commit 86e0225
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 37-50/longest-substring-of-all-vowels-in-order/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "longest-substring-of-all-vowels-in-order"
version = "0.1.0"
authors = ["Checky Hu <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
68 changes: 68 additions & 0 deletions 37-50/longest-substring-of-all-vowels-in-order/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::env;

struct Solution {}

impl Solution {
pub fn longest_beautiful_substring(word: String) -> i32 {
let mut result: i32 = 0;
let mut status: (char, usize, usize) = ('b', 0, 0);
for (i, c) in word.chars().enumerate() {
let is_valid: bool = match status.0 {
'a' => c == 'a' || c == 'e',
'e' => c == 'e' || c == 'i',
'i' => c == 'i' || c == 'o',
'o' => c == 'o' || c == 'u',
'u' => c == 'u',
_ => false,
};
if is_valid {
status.0 = c;
status.2 = i;
} else {
if status.0 == 'u' {
let t: i32 = (status.2 - status.1) as i32 + 1;
if t > result {
result = t;
}
}
if c == 'a' {
status.0 = 'a';
} else {
status.0 = 'b';
}
status.1 = i;
status.2 = i;
}
status.2 = i;
}
if status.0 == 'u' {
let t: i32 = (status.2 - status.1) as i32 + 1;
if t > result {
result = t;
}
}
result
}
}

fn main() {
let mut ret: usize = 0;
for (index, arg) in env::args().enumerate() {
match index {
0 => (),
_ => {
ret += 1;
let word: String = arg;
println!(
"Longest beautiful substring: {}",
Solution::longest_beautiful_substring(word)
);
break;
}
}
}

if 0 == ret {
println!("Require at least 1 parameter.");
}
}

0 comments on commit 86e0225

Please sign in to comment.