Reverse Only the Vowels of a String

Implement reverseOnlyVowels

Given a string s, reverse only the vowels in it and return the result — every consonant stays exactly where it is. Collecting every vowel into a separate list and handing them back out in reverse order works, but it costs an extra list the size of the vowel count. The two-pointerTwo PointerUsing two indices that move through one or more sequences to avoid redundant re-scanning. technique swaps vowels directly in place: walk pointers inward from both ends, sliding each one forward or backward until it lands on a vowel, then swap the pair and keep going. No extra storage is needed — the vowels trade positions as the pointers move toward the middle.

Example 1:

Input: s = "hello"

Output: "holle"

Example 2:

Input: s = "leetcode"

Output: "leotcede"

Example 3:

Input: s = "aA"

Output: "Aa"

+ 8 hidden test cases run on Submit.

Constraints:

  • 1 ≤ s.length ≤ 3 × 10⁵
  • s consists of printable ASCII characters

s =

hello