Reverse Vowels of a String

Easy · Two Pointers · Two Pointers · String

Problem

Given a string s, reverse the order of only its vowels and return the result. Every other character stays in its original position.

The vowels are a, e, i, o and u, in either lowercase or uppercase. A vowel keeps its own case when it moves.

Examples

Input: s = "programming"
Output: "prigrammong"

The vowels o, a, i are written back as i, a, o; every consonant stays where it was.

Input: s = "EchO"
Output: "OchE"

E and O swap, and each keeps its own case.

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

There are no vowels (y is not one), so nothing moves.

Constraints

Approaches

Follow-up

What if the string were a stream too long to hold, read once from each end? The same two pointers work on any sequence with random access from both ends, such as a file you can seek in: read a vowel from the front, find one from the back, and write both swapped. Without access from the back you would have to buffer the vowels, which is the stack approach.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…