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, UThis 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 charactersApproach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Extract all vowels
- Reverse vowel list
- Replace vowels back into string
This approach works but uses extra space.
Steps
- Traverse string.
- Store vowels separately.
- Reverse vowel list.
- Traverse string again.
- Replace vowels in reversed order.
- 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:
- Place:
- left pointer at beginning
- right pointer at end
- Move pointers until vowels are found.
- Swap vowels.
- Continue traversal.
This avoids extra vowel storage.
Steps
- Convert string into array.
- Initialize:
- left = 0
- right = n - 1
- Move left until vowel found.
- Move right until vowel found.
- Swap vowels.
- Move pointers inward.
- 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
- String contains no vowels
- String contains only vowels
- Uppercase vowels present
- String contains one character
- Consecutive vowels present
Why This Problem is Important
This problem helps in understanding:
- Two Pointer Technique
- Conditional traversal
- Character swapping
- String manipulation
- In-place modification
It is one of the most important string two-pointer interview problems.
Real-World Applications
Character swapping concepts are used in:
- Text editors
- Compiler design
- Data formatting
- Encryption systems
- Parsing engines
Common Mistakes
- Incorrect vowel checking
- Wrong pointer movement
- Forgetting uppercase vowels
- Swapping non-vowel characters
Interview Tips
Interviewers often expect:
- Two Pointer optimization
- In-place swapping
- Efficient traversal explanation
Always explain why non-vowel characters remain unchanged.
Related Questions
- Reverse String
- Valid Palindrome II
- Merge Strings Alternately
- Remove Spaces
- 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.