← All problems

5. Reverse Vowels of a String

EasyArray / StringOriginal on LeetCode ↗

Return s with only its vowels reversed: the first vowel swaps with the last vowel, the second with the second-to-last, and so on. Everything else stays where it is.

The vowels are a, e, i, o, u, in lowercase or uppercase.

Constraints

  • 1 <= len(s) <= 300000
  • s contains printable ASCII characters.

Examples

Example 1
Input:  s = "sunrise"
Output: "senrisu"
Example 2
Input:  s = "Programming"
Output: "Prigrammong"
Example 3
Input:  s = "rhythm"
Output: "rhythm"

Hints

Hint 1

Use two pointers, one from each end, that skip over consonants.

Hint 2

Vowels can be upper or lower case.

Python 3

Run checks the examples · Submit checks every test. The first run downloads Python (about 10 MB) and takes a few seconds.