345 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.
Example 1: Given s = "hello", return "holle".
Example 2: Given s = "leetcode", return "leotcede".

Solution:

class Solution {
public:
    string reverseVowels(string s) {
        string vowel = "aeiouAEIOU";
        vector<int> vowel_list;
        if ( s.size() == 0 ) return s;
        int i = 0, j = s.size()-1;
        while ( i <= j ) {
            if (vowel.find(s[i]) == -1 ) {i++; continue;}
            if (vowel.find(s[j]) == -1 ) {j--; continue;}
            swap(s[i++], s[j--]);
        }
        return s;
    }
};

Note: Find a substring in a string:

    s.find(c)

The function returns the first position of substring c. Otherwise it returns -1.

results matching ""

    No results matching ""