8ms C straight-forward solution


  • 0
    char* reverseVowels(char* s) {
        int i;
        int idx;
        int cnt = 0;
        int vowel = 0;
        char* rev;
    
        while(s[cnt]!='\0'){
            cnt++;
        }
        rev = (char*)malloc(sizeof(char)*(cnt+1));
        if(rev == NULL) exit(0);
        idx = cnt-1;
    
        for(i=0; i<cnt; i++){
            while(s[idx]!='a' && s[idx]!='e' && s[idx]!='i' && s[idx]!='o' && s[idx]!='u'
                && s[idx]!='A' && s[idx]!='E' && s[idx]!='I'&& s[idx]!='O' && s[idx]!='U'){
                idx--;
            }
            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'){
                rev[i] = s[i];
            }
            else{
                rev[i] = s[idx];
                idx--;
            }  
        }
        rev[cnt]='\0';
        for(i=0; i<cnt; i++) s[i] = rev[i];
        free(rev);
    
        return s;
    }

Log in to reply
 

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