Easy understand Cpp Solution


  • 0
    N
    class Solution {
    public:
        string reverseVowels(string s) {
            string tempS;
            vector<int> index;
            for(int i = 0; i < s.length();i++)
            {
                if(s[i] == 'a' || s[i] == 'e'|| s[i] == 'i'|| s[i] == 'o'|| s[i] == 'u'
                 || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
                {
                    tempS += s[i];
                    index.push_back(i);
                }
            }
            
            reverse(tempS.begin(),tempS.end());
         
            for(int i = 0; i < index.size();i++)
            {
                s[index[i]] = tempS[i];
            }
            
            return s;
        }
    };

Log in to reply
 

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