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”.
Note:
The vowels does not include the letter “y”.
class Solution {
public:
bool isVowels(char ch){
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') return true;
return false;
}
string reverseVowels(string s) {
for(int i = 0, j = s.length() - 1; i <= j;){
if(isVowels(s[i]) && isVowels(s[j])){
swap(s[i], s[j]);
++i; --j;
} else if(isVowels(s[i]))
--j;
else if(isVowels(s[j]))
++i;
else {
++i; --j;
}
}
return s;
}
};