345. Reverse Vowels of a String - cocoder39/coco39_LC GitHub Wiki

345. Reverse Vowels of a String

tip: use vowels.find(s[end]) == string::npos

string reverseVowels(string s) {
        const string vowels("aeiouAEIOU");
        
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            if (vowels.find(s[start]) == string::npos)   start++;
            else if (vowels.find(s[end]) == string::npos) end--;
            else    swap(s[start++], s[end--]);
        }
        
        return s;
    }