Reverse Vowels of a String

Reverse Vowels of a String is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the String Reversal subpattern.

Problem statement

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s = "IceCreAm"

Output: "AceCreIm"

Explanation:

The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".

Example 2:

Input: s = "leetcode"

Output: "leotcede"

Constraints:

- 1 <= s.length <= 3 105

- s consist of printable ASCII characters.

Topics and companies

Topics: Two Pointers, String.

Reported in interviews at Facebook, Google, Amazon.

How to practise Reverse Vowels of a String on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Reverse Vowels of a String in the code editor, or read the Reverse Vowels of a String editorial for a worked solution with its approach and complexity analysis.

Where Reverse Vowels of a String sits in the DSA pattern sheet

Problems related to Reverse Vowels of a String

Other problems that use the same String Reversal and Two Pointers techniques:

Preparing your workspace

Progress is saved as you study.