easy

Reverse Vowels of a String

Reverse only the vowels of a string, leaving every other character in place.

1. Define the problem

Reverse Vowels of a String

Given a string s, reverse only the vowels of the string, leaving all other characters in their original positions. Vowels are 'a', 'e', 'i', 'o', 'u' and their uppercase forms. Use two pointers starting at the left and right ends, skipping non-vowel characters and swapping vowels as the pointers converge toward the middle.

Constraints

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

Example

Inputs = "hello"
Output"holle"

Explanation The vowels e and o swap positions while h, l, l stay in place.

2. Visualize the solution

Converge two pointers, swapping vowels as they meet

Converge two pointers, swapping vowels as they meet
Statusinit

left skips index 0 ('h', not a vowel) and lands on index 1 ('e'). right lands on index 4 ('o'). Both are vowels — about to swap.

What happens in this step

left skips index 0 ('h', not a vowel) to land on index 1 ('e')
right = 4 (value 'o'), already a vowel

Both left and right sit on vowels, so they are about to swap.
Step 1 of 3

Steps to visualize

  1. Place one pointer at the start and one at the end of the string.
  2. Move the left pointer right while it sits on a non-vowel.
  3. Move the right pointer left while it sits on a non-vowel.
  4. Swap the vowels at both pointers, then move both one step toward the middle.
  5. Repeat until the pointers meet or cross.
3. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Converge two pointers, swapping vowels as they meet
Statusinit

left skips index 0 ('h', not a vowel) and lands on index 1 ('e'). right lands on index 4 ('o'). Both are vowels — about to swap.

What happens in this step

left skips index 0 ('h', not a vowel) to land on index 1 ('e')
right = 4 (value 'o'), already a vowel

Both left and right sit on vowels, so they are about to swap.
Step 1 of 3
4. Solution

Solution

solution.tsTypeScript
function reverseVowels(s) {
  const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
  const chars = s.split('');
  let left = 0;
  let right = chars.length - 1;

  while (left < right) {
    while (left < right && !vowels.has(chars[left])) {
      left++;
    }
    while (left < right && !vowels.has(chars[right])) {
      right--;
    }
    if (left < right) {
      const temp = chars[left];
      chars[left] = chars[right];
      chars[right] = temp;
      left++;
      right--;
    }
  }

  return chars.join('');
}
Time
O(n)
Space
O(n)
5. Test cases

Test cases

InputExpectedCovers
s = "hello""holle"example from the docstring
s = "xyz""xyz"no vowels present, string is unchanged
s = "aeiou""uoiea"every character is a vowel
s = "Aa""aA"mixed case, adjacent vowels of different case
s = "leetcode""leotcede"vowels and consonants interleaved throughout the string
s = "b""b"smallest valid input, a single non-vowel character
s = "ab""ab"only one vowel present, nothing to swap with