When learning the Two Pointer technique, some problems don’t fail completely — they fail quietly.
Reverse Vowels of a String was one of those problems for me.My code worked for many cases, but the logic had a hidden flaw.
This article explains:
- My original approach
- What I missed
- The correction
- What this problem taught me about two pointers
🧩 Problem: Reverse Vowels of a String
Description
You are given a string.
Reverse only the vowels while keeping all other characters in the same position.
Example
Input: "leetcode"
Output: "leotcede"
✍️ My Initial Approach (Two Pointer)
I used two pointers:
left at the start
right at the end
My idea in English
If both characters are vowels → swap them
If left is not vowel → move left
Else → move right
This feels logical — and mostly works.
💻 My Original Code
bool isVowel(char c){
if(tolower(c) == 'a' || tolower(c) == 'e' ||
tolower(c) == 'i' || tolower(c) == 'o' ||
tolower(c) == 'u')
return true;
return false;
}
int main() {
string s = "leetcode";
int left = 0, right = s.size() - 1;
while(left < right){
if(isVowel(s[left]) && isVowel(s[right])){
swap(s[left], s[right]);
left++;
right--;
}
else if(!isVowel(s[left]) && isVowel(s[right]))
left++;
else
right--;
}
cout << s;
}
⚠️ What I Missed (Important Insight)
❌ Missing Case
Note : What if both characters are NOT vowels?
In my code:
else always moves right-- . Even when both left and right are consonants
This causes:
Unbalanced pointer movement. Hard-to-detect logic bugs in edge cases
✅ Correct Pointer Movement Rule
We must handle all four cases clearly:
| Left | Right | Action |
|---|---|---|
| vowel | vowel | swap, move both |
| not vowel | vowel | move left |
| vowel | not vowel | move right |
| not vowel | not vowel | move both |
This makes pointer movement intentional, not accidental.
✅ Corrected Code (Clear & Safe)
while(left < right) {
if(!isVowel(s[left])) {
left++;
}
else if(!isVowel(s[right])) {
right--;
}
else {
swap(s[left], s[right]);
left++;
right--;
}
}
This version:
- Handles all cases explicitly
- Is easier to reason about
- Avoids hidden bugs
🧠 What I Learned From This Problem
1️⃣_ Two Pointer Is About Movement Logic_
Not just swapping —
but why a pointer moves.
2️⃣ Always Cover All Logical Cases
If you use else, ask yourself:
“What exact condition is this handling?”
If you can’t answer clearly — the logic is incomplete.
3️⃣ Readability > Cleverness
The corrected code looks simpler because:
- Each pointer moves for a single clear reason
- No condition overlaps
- Interviewers prefer this.
❌ Common Beginner Mistakes (I Made Them)
❌ Combining multiple cases into one else
❌ Assuming “it works for sample” means “it’s correct”
❌ Not writing pointer rules before coding
🎯 Final Two Pointer Rule I Took Away
Move pointers until they point to useful elements.
Only then perform the operation.
This rule applies to:
Reverse vowels
Palindrome checks
Two Sum (sorted)
Container With Most Water
Top comments (2)
Hii @nithya_dharshiniofficial ,
Really liked your breakdown of pointer movement 👏
Another approach I found interesting is moving both pointers until they hit vowels first, then swapping. It keeps the movement logic very clean and avoids multiple condition checks. Two-pointer problems really teach precision in movement.
This part taught me how to approach two pointer problems quite meaningful
Thank you for sharing this @nithya_dharshiniofficial