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.
- Place a
leftpointer at the start and arightpointer at the end. - Advance
leftuntil it points to a vowel. - Move
rightbackward until it points to a vowel. - If they haven't crossed each other, swap the vowels and move both pointers inward.
- Repeat until the search is complete, then print the final array as a space-separated string.
01EXAMPLE 1
Input
["h", "e", "l", "l", "o"]Output
h o l l eExplanation: e and o swapped.
Constraints
- O(n) time complexity.
ArraysTwo Pointers
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
chars["h","e","l","l","o"]
Expected Output
h o l l e
Click RUN to test your solution