292/670

292. Reverse Vowels of a String

Easy

Given a string s, reverse only its vowels: the vowels must end up in the opposite of their original order, while every consonant, digit, and symbol stays exactly where it was.

The vowels are a, e, i, o, u — in both lower and upper case. y never counts. A vowel keeps its own case when it moves; only positions change.

The function receives s and returns the transformed string. Aim for one pass with two pointers that walk in from the ends and skip over everything that isn't a vowel.

Example 1:

Input: s = "hello"

Output: "holle"

Explanation: The vowels are e (index 1) and o (index 4); swapping them gives holle. The consonants h, l, l never move.

Example 2:

Input: s = "Apple"

Output: "epplA"

Explanation: The vowels A and e trade places, each keeping its own case: A moves to the end, e to the front.

Constraints:

  • 1 ≤ s.length ≤ 3 × 10⁵
  • s consists of printable ASCII characters (no whitespace)
  • Vowels are aeiou and AEIOU; y is not a vowel

Hints:

Two passes work: collect every vowel into a list, then walk the string again, replacing each vowel with the last uncollected one from your list.

One pass: put a pointer at each end. March the left pointer forward until it hits a vowel and the right pointer backward until it hits one, swap the two, step both inward, and repeat until the pointers cross. Remember the uppercase vowels in your membership test.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: s = "hello"

Expected output: "holle"