345. Reverse Vowels of a String

Reverse Vowels of a String (LeetCode 345) 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.

How to solve Reverse Vowels of a String

Reverse Vowels of a String is a String Reversal Using Two Pointers problem, part of Two Pointers. Master string reversal techniques including reverse string, reverse words, and reverse vowels using two pointers.

Work towards the solution in these steps:

  1. Consider which data structure or approach allows efficient traversal from both ends of a sequence simultaneously.
  2. Identify all vowels in the string and think about how you might swap these vowels without affecting other characters.
  3. Try using two pointers: one starting at the beginning and one at the end of the string, moving towards each other to find vowels.

The reference solution runs in O(n) time and O(1) space.

The Reverse Vowels of a String solution and editorial walks through the full approach with reference code.

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 10^5

- 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:

Reverse Vowels of a String FAQ

What is the LeetCode number of Reverse Vowels of a String?

Reverse Vowels of a String is LeetCode problem 345, rated Easy.

What pattern is used to solve Reverse Vowels of a String?

Reverse Vowels of a String is a String Reversal Using Two Pointers problem. It belongs to the Two Pointers pattern, in the String Reversal subpattern.

What is the time complexity of Reverse Vowels of a String?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Reverse Vowels of a String?

Reverse Vowels of a String has been reported in interviews at Facebook, Google, Amazon.

Preparing this workspace.