1184. Shift the Vowels

Easy1.5 points estimatedStringTwo Pointers

A poet is experimenting with a sound-shifting game. In the printable ASCII string s, the vowels are a, e, i, o and u in lower or upper case. Read the vowels from left to right, then rotate that list to the right by one place: the last vowel moves into the first vowel slot and every other vowel moves into the next vowel slot.

Write the vowels back into the vowel positions of s. Each vowel moves as it is, so it keeps its own letter and its own case, and every non-vowel character stays exactly where it was. Return the new string. If s has fewer than two vowels it comes back unchanged.

Constraints: 1 <= s.length <= 10^4 and s uses printable ASCII only. Collect the vowel positions once and write the result in O(n) time with O(n) space.

Example 1

Input:
s = "leetcode!"
Output:
"leetcedo!"
Explanation:

The vowels of "leetcode!" are e, e, o, e; rotated right they are e, e, e, o, and writing them into positions 1, 2, 5, 7 gives "leetcedo!".

Example 2

Input:
s = "Apple Pie"
Output:
"epplA Pei"
Explanation:

The vowels A, e, i, e rotate right to e, A, e, i, and writing them into positions 0, 4, 7, 8 gives "epplA Pei".

Example 3

Input:
s = "rhythm"
Output:
"rhythm"
Explanation:

There are no vowels, so the string is returned unchanged.

Constraints

1 ≤ s.length ≤ 104

s is printable ASCII

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…