AAlgoLoopSpaced repetition for LeetCode
EASYTwo PointersLeetCode ↗

Reverse Vowels of a String

The key idea

Only the vowels move; every consonant stays put. Walk two pointers from both ends inward, and whenever both land on a vowel, swap them. This reverses the vowels in place without touching anything else.

Problem

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.

Constraints

Examples

Input: s = "IceCreaM" Output: "AceCreaI"
Input: s = "leetcode" Output: "leotcede"

Complexity

Time: O(n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Two Pointers problems