diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/reverse_vowel_string.rs | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/reverse_vowel_string.rs')
-rw-r--r-- | other/reverse_vowel_string.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/other/reverse_vowel_string.rs b/other/reverse_vowel_string.rs new file mode 100644 index 0000000..4ae282d --- /dev/null +++ b/other/reverse_vowel_string.rs @@ -0,0 +1,33 @@ +struct Solution; + +impl Solution { + pub fn reverse_vowels(s: String) -> String { + let (mut i, mut j) = (0, s.len().checked_sub(1).unwrap_or(0)); + let mut cs = s.chars().collect::<Vec<char>>(); + while i < j { + if !Self::is_vowel(cs[i].to_ascii_lowercase()) { + i += 1; + continue + } + if !Self::is_vowel(cs[j].to_ascii_lowercase()) { + j -= 1; + continue + } + cs.swap(i, j); + i += 1; + j -= 1; + } + cs.iter().collect() + } + fn is_vowel(c: char) -> bool { + match c { + 'a' | 'e' | 'i' | 'o' | 'u' => true, + _ => false + } + } +} + +fn main() { + let s = "hello".to_string(); + print!("{}", Solution::reverse_vowels(s)); +} |