[LeetCode] Reverse Vowels of a String

345. Reverse Vowels of a String

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
string reverseVowels(string s) {
unordered_set<char> us{'a','e','i','o','u'};
vector<char> st;
for(int i = 0; i < s.length(); i++) if(us.count(tolower(s[i]))) st.push_back(s[i]);
for(int i = 0; i < s.length(); i++) if(us.count(tolower(s[i]))) {
s[i] = st.back();
st.pop_back();
}
return s;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/04/PS/LeetCode/reverse-vowels-of-a-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.