clueless coding // TODO: be smarter

LeetCode Reverse Vowels of a String: CPP Solution



TL;DR Code Solution


string reverseVowels(string s) {

    int i = 0;
    int j = s.length() - 1;

    while (i < j) {
        if (!isVowel(s[i])) {
            i++;
        }
        else if (!isVowel(s[j])) {
            j--;
        }
        else {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }

    return s;

}

bool isVowel(char c) {
    char in = tolower(c);
    if (in == 'a' || in == 'e' || in =='i' || in =='o' || in =='u') {
        return true;
    }
    return false;
}