Reverse Vowels in Array

EasyAcc. 92.1%
+15 XP 5

The Selective Reversal

Using two pointers, we can reverse specific parts of a collection while leaving others untouched. In this mission, you must reverse only the vowels (a, e, i, o, u) while keeping the consonants and other symbols in their original positions.

The Assignment

Your mission is to reverse the vowel sequence. Your function receives an array of characters chars.

  1. Place a left pointer at the start and a right pointer at the end.
  2. Advance left until it points to a vowel.
  3. Move right backward until it points to a vowel.
  4. If they haven't crossed each other, swap the vowels and move both pointers inward.
  5. Repeat until the search is complete, then print the final array as a space-separated string.

01EXAMPLE 1

Input["h", "e", "l", "l", "o"]
Outputh o l l e

Explanation: e and o swapped.

Constraints

  • O(n) time complexity.
ArraysTwo Pointers
JavaScript
Loading...
1 Hidden

Input Arguments

chars["h","e","l","l","o"]

Expected Output

h o l l e

Click RUN to test your solution