Skip to main content
Back to problems
Leetcode
Medium
Strings
Sorting
Two Pointers
Sort Vowels in a String

Reorder only the vowels in a string while keeping all consonants in their original positions.

Acceptance 0%
Problem Statement

Problem

Given a string s, sort all vowels in s in nondecreasing order while leaving every non-vowel character exactly where it is.

Vowels are a, e, i, o, u, and their uppercase variants.

Return the modified string after the vowels have been rearranged.

Goal

Only the vowels may move. All other characters must stay in the same indices they originally occupied.

Input Format

  • A single string s.
  • s may contain lowercase letters, uppercase letters, and possibly other characters depending on the test setting.

Treat vowels case-sensitively for ordering, but identify both lowercase and uppercase vowels as vowels.

Output Format

  • Return a string of the same length as s.
  • The vowels in the returned string must appear in sorted order from left to right.
  • Non-vowel characters must remain unchanged in their original positions.

Constraints

  • 1 <= s.length <= $10^{5}$
  • The relative order of non-vowel characters must not change.
  • Only the vowel characters are rearranged.
  • Use an approach that is efficient enough for linear or near-linear processing of the string.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "lEetcOde"

Output

"lEOtcede"

Explanation

The vowels are E, e, O, e. Sorted by character order, they become E, O, e, e. Putting them back into the vowel slots gives lEOtcede.

Example 2

Input

s = "leetcode"

Output

"leetcedo"

Explanation

The vowels are e, e, o. Sorted order is still e, e, o, so only their positions are rearranged accordingly.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.