C++ two pointers, one loop


  • 0
    H
    class Solution {
    private:
        const string _vowels = "aeiouAEIOU";
        
    public:
        string reverseVowels(string s) {
            int first = 0;
            int last = s.size()-1;
            while (first < last) {
                if (!vowels(s[first])) {
                    first++;
                    continue;
                }
                if (!vowels(s[last])) {
                    last--;
                    continue;
                }
                swap(s[first++], s[last--]);
            }
            return s;
        }
        
        bool vowels(const char c) {
            return any_of(_vowels.begin(), _vowels.end(), [c](const char v) {return v == c;});
        }
    };
    

Log in to reply
 

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