C++ Solution with two pointers


  • 0
    Y

    C++ solution with two pointers. move pointers to vowels. then swap them.

    class Solution {
    public:
    string reverseVowels(string s) {

        set<char> vowel = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        int begin = 0;
        int end = s.length()-1;
        while (begin < end) {
            if (vowel.find(s[begin]) == vowel.end()) {
                begin++;
                continue;
            }
            if (vowel.find(s[end]) == vowel.end()) {
                end--;
                continue;
            }
            char temp = s[begin];
            s[begin] = s[end];
            s[end] = temp;
            begin++;
            end--;
        }
        return s;
        
    }
    

    };


Log in to reply
 

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