Reverse Vowels of a String

Easy

Topics
StringTwo Pointers

Given a string s, reverse only the vowels (a e i o u, both cases) and return the result.

Example 1

inhello
outholle
vowel
Only the shaded characters move, and they keep their positions while exchanging contents.
The string before and after, with the vowel positions shaded.
Input:  s = "hello"
Output: "holle"

Example 2

inleetcode
outleotcede
vowel
Input:  s = "leetcode"
Output: "leotcede"

Constraints

  • 1 <= s.length <= 3*10^5
Editorial — intuition, approach, complexity and pitfalls

Intuition

Only the vowels move, and they swap with each other from the outside in — consonants stay exactly where they are. So this is the two-pointer reversal, with a filter: advance each pointer until it lands on a vowel, then swap the two vowels and step both inward.

The structure is worth noticing because it generalises: "reverse only the elements satisfying P, leaving the rest in place" is always two pointers that skip non-matching positions.

Approach

  1. A set of the ten vowels, both cases, for O(1) membership.
  2. left and right at the ends. If left is not a vowel, advance it; else if right is not a vowel, retreat it; else swap and move both.

The three-branch if/elif/else matters: advance only one pointer per iteration until both are on vowels, then swap.

Dry Run

s = "leetcode"   ->  ['l','e','e','t','c','o','d','e']

left=0 'l' not vowel  -> left=1
left=1 'e', right=7 'e'  both vowels -> swap (no visible change)
                                        left=2 right=6
left=2 'e', right=6 'd' not vowel -> right=5
left=2 'e', right=5 'o'  both vowels -> swap
     ['l','o','e','t','c','e','d','e']   left=3 right=4
left=3 't' not vowel -> left=4
left=4 == right=4, loop ends

answer "leotcede"

Complexity

  • Time — O(n): each pointer only ever moves inward, so together they traverse the string once.
  • Space — O(n) in Python, because strings are immutable and the characters must be copied into a list to be swapped. In a language with mutable strings or on a char array this is O(1). The vowel set is O(1), being ten fixed entries.

Common Pitfalls

  • Forgetting uppercase vowels. The set needs both cases, or lowercase the character before testing.
  • Advancing both pointers in the same iteration while skipping, which can step over a vowel.
  • Collecting the vowels, reversing them, and writing them back — correct, and O(n) extra, where two pointers need none beyond the mutable buffer.
  • Treating 'y' as a vowel. It is not, for this problem.

Full Solution — all languages

class Solution:
    def reverseVowels(self, s):
        v = set("aeiouAEIOU")
        a = list(s)
        left, right = 0, len(a) - 1
        while left < right:
            if a[left] not in v:
                left += 1
            elif a[right] not in v:
                right -= 1
            else:
                a[left], a[right] = a[right], a[left]
                left += 1
                right -= 1
        return "".join(a)
SensAI
Beta
Tap the mic to stop voice mode