Community Problem

Reverse Vowels of a String

Difficulty: Easy

Reverse only the vowels of a string while keeping consonants and punctuation in place.

Reverse Vowels of a String

Reverse only the vowels of a string while keeping consonants and punctuation in place.

EASY
Free
strings
two-pointers
meinakamura

By @meinakamura

January 12, 2026

·

Updated May 20, 2026

788 views

5

4.3 (13)

Saw this on a screening loop two weeks ago, and the candidate spent eight minutes building a frequency map before realizing the problem was just Reverse String with a filter. The trap is that the wording ("reverse only the vowels") nudges you toward sorting or counting, when the real shape is two pointers that skip consonants. Worth keeping in your warm-up rotation right after Reverse String.

Reverse Vowels of a String

Given a string s, reverse only the vowels of the string and return it. The vowels are 'a', 'e', 'i', 'o', 'u', in both lowercase and uppercase. All other characters keep their original positions; the relative order of vowels themselves is reversed.

Examples

Example 1:

  • Input: s = "hello"
  • Output: "holle"
  • Explanation: The vowels are 'e' and 'o' at indices 1 and 4. Swapping them produces "holle".

Example 2:

  • Input: s = "leetcode"
  • Output: "leotcede"
  • Explanation: Vowels are e, e, o, e at indices 1, 2, 6, 7. Reversed sequence is e, o, e, e, which yields "leotcede".

Example 3:

  • Input: s = "AEIOU"
  • Output: "UOIEA"
  • Explanation: Every character is a vowel, so the result equals the full reverse.

Example 4:

  • Input: s = "sky"
  • Output: "sky"
  • Explanation: No vowels in the string, so the output equals the input.

Constraints

  • 1 <= s.length <= 3 * 10^5
  • s consists of printable ASCII characters.

Follow-up

How would the algorithm change if the vowel set were configurable (for instance, treating 'y' as a vowel in some dialects)?

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems