Last active
September 11, 2025 15:39
-
-
Save tatsuyax25/1861e03167829192250f501932b866e8 to your computer and use it in GitHub Desktop.
Given a 0-indexed string s, permute s to get a new string t such that: All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
The vowels must be s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Sorts all vowels in the input string in non-decreasing order, | |
| * while keeping consonants and other characters in their original positions. | |
| * | |
| * @param {string} s - The input string to process. | |
| * @return {string} - The resulting string with sorted vowels. | |
| */ | |
| const sortVowels = function(s) { | |
| const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); | |
| // Extract vowels from the input string | |
| const extractedVowels = [...s].filter(char => vowels.has(char)); | |
| // Sort vowels lexicographically | |
| extractedVowels.sort(); | |
| // Reconstruct the string, replacing vowels with sorted ones | |
| let vowelIndex = 0; | |
| let result = ''; | |
| for (const char of s) { | |
| if (vowels.has(char)) { | |
| result += extractedVowels[vowelIndex++]; | |
| } else { | |
| result += char; | |
| } | |
| } | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment