Skip to content
On this page

345. Reverse Vowels of a String share

Problem Statement

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

 

Example 1:

Input: s = "hello"
Output: "holle"

Example 2:

Input: s = "leetcode"
Output: "leotcede"

 

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consist of printable ASCII characters.

Solution:

rs
impl Solution {
    pub fn reverse_vowels(s: String) -> String {
        let mut s = s.into_bytes();
        let (mut i, mut j) = (0, s.len() - 1);

        while i < j {
            if !Self::is_vowel(s[i] as char) {
                i += 1;
                continue;
            }
            if !Self::is_vowel(s[j] as char) {
                j -= 1;
                continue;
            }
            s.swap(i, j);
            i += 1;
            j -= 1;
        }
        String::from_utf8(s).unwrap()

        // let mut chars: Vec<char> = s.chars().collect();
        // let (mut i, mut j) = (0, chars.len() - 1);

        // while i < j {
        //     if !Self::is_vowel(chars[i]) {
        //         i += 1;
        //         continue;
        //     }
        //     if !Self::is_vowel(chars[j]) {
        //         j -= 1;
        //         continue;
        //     }
        //     chars.swap(i, j);
        //     i += 1;
        //     j -= 1;
        // }

        // chars.into_iter().collect()
    }

    fn is_vowel(ch: char) -> bool {
        match ch {
            'a' | 'e' | 'i' | 'o' | 'u' | 'A' | 'E' | 'I' | 'O' | 'U' => true,
            _ => false,
        }
    }
}

...


Released under the MIT License.