PU Reverse Vowels of a String

Jan 01, 1970

Write a function that takes a string as input and reverse only the vowels of a string.

  • The vowels does not include the letter "y".

Example:

  • Given s = "hello", return "holle".
  • Given s = "leetcode", return "leotcede".

C Solution:

char* reverseVowels(char* s) {
    char *l = s, *r = s + strlen(s) - 1;
    char flag[256] = {0};
    flag['a'] = flag['A'] = 1;
    flag['e'] = flag['E'] = 1;
    flag['i'] = flag['I'] = 1;
    flag['o'] = flag['O'] = 1;
    flag['u'] = flag['U'] = 1;
    while (1) {
        while (l < r && !flag[*l]) l++;
        while (l < r && !flag[*r]) r--;
        if (l >= r) break;
        char c = *l;
        *l++ = *r;
        *r-- = c;
    }
    return s;
}

Summary:

  • nothing to say.

LeetCode: 345. Reverse Vowels of a String