Possibly fastest, safest and cleanest solution

  • 1

    fast - no hash set lookup, no string lookup, real O(1) vowel test
    short and clean - no tedious switch cases, no tedious if conditions, no extra function
    safe - no out-of-bounds array index which is a common issue for nested while loops

    string reverseVowels(string s) {
        array<bool, 128> vowels = {0};
        for (char c : "aeiouAEIOU")
            vowels[c] = true;
        for (int i = 0, j = s.size() - 1; i < j;) {
            if (!vowels[s[i]])
            else if (!vowels[s[j]])
                swap(s[i++], s[j--]);
        return s;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.