345. Reverse Vowels of a String
Input: "hello"
Output: "holle"Input: "leetcode"
Output: "leotcede"string reverseVowels(string s) { // time: O(n); space: O(1)
int n = s.length();
if (n <= 1) return s;
string vowels = "aeiouAEIOU";
int i = 0, j = n - 1;
while (i < j) {
while (i < j && vowels.find(s[i]) == string::npos) ++i;
while (i < j && vowels.find(s[j]) == string::npos) --j;
swap(s[i++], s[j--]);
}
return s;
}Last updated