Introduction

The Reverse Vowels of a String problem involves reversing only the vowels in a string while keeping all other characters in their original positions.

Given a string s, the task is to:

  • reverse only vowels
  • keep consonants unchanged

Vowels include:

a, e, i, o, uA, E, I, O, U

This problem helps in understanding:

  • Two Pointer Technique
  • conditional traversal
  • character swapping
  • string manipulation

Example

Input:s = "hello"
Output: "holle"
Explanation: Vowels:
e,o
After reversing:
o,e
Input: s = "leetcode"
Output:
"leotcede"
Explanation:
Vowels are reversed
while consonants remain unchanged.

Constraints

 1 <= s.length <= 3 * 10^5s contains printable ASCII characters

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Extract all vowels
  2. Reverse vowel list
  3. Replace vowels back into string

This approach works but uses extra space.

Steps

  1. Traverse string.
  2. Store vowels separately.
  3. Reverse vowel list.
  4. Traverse string again.
  5. Replace vowels in reversed order.
  6. Return modified string.

Dry Run

Input:s = "hello"
Extract vowels:
[e,o]
Reverse vowels:
[o,e]
Replace vowels: h o l l e
Final Result:
"holle"

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
String is traversed multiple times.
Space Complexity: O(n)
Explanation:
Extra vowel storage is used.

Approach 2 : Optimized Solution (Two Pointer Technique)

Explanation

The optimized solution uses the Two Pointer Technique.

The idea is:

  1. Place:
    • left pointer at beginning
    • right pointer at end
  2. Move pointers until vowels are found.
  3. Swap vowels.
  4. Continue traversal.

This avoids extra vowel storage.

Steps

  1. Convert string into array.
  2. Initialize:
    • left = 0
    • right = n - 1
  3. Move left until vowel found.
  4. Move right until vowel found.
  5. Swap vowels.
  6. Move pointers inward.
  7. Return modified string.

Dry Run

Input:s = "hello"
Initially: left = 0 → h
right = 4 → o
Move left left = 1 → e
Swap vowels:
h o l l e
Move pointers
Traversal completed
Final Result: "holle"

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each character is processed once.

Space Complexity: O(1)
Explanation: Swapping is performed in-place.

Edge Cases

  1. String contains no vowels
  2. String contains only vowels
  3. Uppercase vowels present
  4. String contains one character
  5. Consecutive vowels present

Why This Problem is Important

This problem helps in understanding:

  1. Two Pointer Technique
  2. Conditional traversal
  3. Character swapping
  4. String manipulation
  5. In-place modification

It is one of the most important string two-pointer interview problems.

Real-World Applications

Character swapping concepts are used in:

  1. Text editors
  2. Compiler design
  3. Data formatting
  4. Encryption systems
  5. Parsing engines

Common Mistakes

  1. Incorrect vowel checking
  2. Wrong pointer movement
  3. Forgetting uppercase vowels
  4. Swapping non-vowel characters

Interview Tips

Interviewers often expect:

  1. Two Pointer optimization
  2. In-place swapping
  3. Efficient traversal explanation

Always explain why non-vowel characters remain unchanged.

Related Questions

  1. Reverse String
  2. Valid Palindrome II
  3. Merge Strings Alternately
  4. Remove Spaces
  5. String Compression

Final Takeaway

The Reverse Vowels of a String problem is a fundamental two-pointer string problem that teaches conditional traversal and in-place swapping techniques. Understanding this problem builds a strong foundation for advanced string manipulation and interview problems.