DEV Community

Nithya Dharshini official
Nithya Dharshini official

Posted on

Reverse Vowels of a String — What I Missed, What I Learned (Two Pointer Reflection)

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:

  1. My original approach
  2. What I missed
  3. The correction
  4. 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"

Enter fullscreen mode Exit fullscreen mode

✍️ My Initial Approach (Two Pointer)

I used two pointers:

left at the start
right at the end

Enter fullscreen mode Exit fullscreen mode

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;
}

Enter fullscreen mode Exit fullscreen mode

⚠️ 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--;
    }
}

Enter fullscreen mode Exit fullscreen mode

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:

  1. Each pointer moves for a single clear reason
  2. No condition overlaps
  3. 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 (1)

Collapse
 
charles_kumar_30654e10958 profile image
Charles Kumar

This part taught me how to approach two pointer problems quite meaningful

Thank you for sharing this @nithya_dharshiniofficial