Featured image of post LeetCode 75 - 345. Reverse Vowels of a String

LeetCode 75 - 345. Reverse Vowels of a String

Description

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”

Constraints:

  • 1 <= s.length <= 3 * 10^5
  • s consist of printable ASCII characters.

Solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    string reverseVowels(string s) {
        set<char> words{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        vector<bool> v(s.size(), false);
        queue<char> re;

        for (int i = s.size(); i >= 0; i--) {
            if (words.contains(s[i])) {
                re.push(s[i]);
                v[i] = true;
            }
        }
        for (int i = 0; i < v.size(); ++i) {
            if (v[i] == true) {
                s[i] = re.front();
                re.pop();
            }
        }
        return s;
    }
};