Sort Vowels in a String

Given a 0-indexed string `s`, permute it to get a new string `t` such that every consonant stays in its original place, and the vowels are sorted in nondecreasing order of their ASCII values. That is, for any two vowel positions `i < j`, `t[i]` must not have a higher ASCII value than `t[j]`. The vowels are `a`, `e`, `i`, `o`, `u` in lowercase or uppercase; every other letter is a consonant. Return the resulting string.

30:00

Sort Vowels in a String
medium
Free written solution
Topics
Companies

Given a 0-indexed string s, permute it to get a new string t such that every consonant stays in its original place, and the vowels are sorted in nondecreasing order of their ASCII values. That is, for any two vowel positions i < j, t[i] must not have a higher ASCII value than t[j]. The vowels are a, e, i, o, u in lowercase or uppercase; every other letter is a consonant. Return the resulting string.

Example 1:
Input: {"s":"lEetcOde"}
Output: lEOtcede
Constraints:
  • 1 <= s.length <= 10^5

  • s consists only of letters of the English alphabet in uppercase and lowercase.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"lEetcOde"}

Set up 10 buckets in ASCII order, A E I O U a e i o u. This string has 4 vowels; the 4 consonants never move.

s: lifting vowels into buckets

scanned 0/8

0

l

 

E

 

e

 

t

 

c

5

O

 

d

 

e

vowel buckets in ASCII order (k walks left to right)

65

A

0

k

69

E

0

k

73

I

0

k

79

O

0

k

85

U

0

k

97

a

0

k

101

e

0

k

105

i

0

k

111

o

0

k

117

u

0

k

out =lEetcOde
Variables
VariableValue
vowels4
consonants4
DepthFunction Call
Stack empty
0/16