◻️345. Reverse Vowels of a String (easy)
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"Example 2:
Input: s = "leetcode"
Output: "leotcede"Solutions
Normal
Runtime 5 ms Memory 9.35 MB
// Solution taken from leetcode.
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.length()-1;
while(i < j){
while(i < j && !isVowel(s[i])){
i++;
}
while(i < j && !isVowel(s[j])){
j--;
}
if(i<j){
swap(s[i], s[j]);
i++;
j--;
}
}
return s;
}
private:
bool isVowel(char c){
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
};Own Solution
Runtime 4 ms Memory 9.34 MB
Last updated