← Home

345. Reverse Vowels of a String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
345

This is one of those problems where the clean idea matters more than the amount of code. For 345. Reverse Vowels of a String, the solution in this repository is mainly a two pointers solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are reverseVowels.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 65.37% of C++ online submissions for Reverse Vowels of a String.
02//Memory Usage: 10 MB, less than 81.82% of C++ online submissions for Reverse Vowels of a String.
03
04class Solution {
05public:
06    string reverseVowels(string s) {
07        int l = 0, r = s.size()-1;
08        string vowels = "aeiouAEIOU";
09        char tmp;
10        
11        // cout << s.size() << endl;
12        
13        while(l < r){
14            while(l < s.size() && vowels.find(s[l]) == string::npos){
15                l++;
16            }
17            
18            while(r >= 0 && vowels.find(s[r]) == string::npos){
19                r--;
20            }
21            
22            // cout << l << ", " << r << endl;
23            
24            if(l < r){
25                tmp = s[l];
26                s[l] = s[r];
27                s[r] = tmp;
28            }
29            
30            l++; r--;
31        }
32        
33        return s;
34    }
35};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.