C++ O(n) 9ms easy to understand


  • 0
    L
    class Solution {
    public:
    
        bool help(char s){
            if(s == 'a'|| s == 'e' || s == 'i' || s == 'o' || s == 'u')
                return true;
            if(s == 'A'|| s == 'E' || s == 'I' || s == 'O' || s == 'U')
                return true;
            return false;
        }
        
        string reverseVowels(string s) {
            if(s.empty())
                return "";
            int i = 0;
            int j = s.size()-1;
            while(i < j){
                while(i < j && !help(s[i]))
                    i++;
                while(i < j && !help(s[j]))
                    j--;
                if(i < j){
                    char temp = s[i];
                    s[i++] = s[j];
                    s[j--] = temp;
                }
            }
            return s;
        }
    };
    

Log in to reply
 

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