aboutsummaryrefslogtreecommitdiff
path: root/reverse_vowel_string.rs
blob: 4ae282d376bb63c6e37f0483d435a87d59d37455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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));
}