Clean 9 lines C++ two pointers


  • 0
    string reverseVowels(string s) {
            string::iterator head=s.begin();
            string::iterator tail=s.end()-1;
            unordered_map<char,int>m({{'a',1},{'e',1},{'i',1},{'o',1},{'u',1},{'A',1},{'E',1},{'I',1},{'O',1},{'U',1}});
            while(head-tail<0){
                if(m[*head]!=1) head++;
                if(m[*tail]!=1) tail--;
                if(m[*head]==1&&m[*tail]==1){swap(*head,*tail); head++;tail--;}
            }
            return s;
        }
    

Log in to reply
 

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