Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active September 11, 2025 15:39
Show Gist options
  • Select an option

  • Save tatsuyax25/1861e03167829192250f501932b866e8 to your computer and use it in GitHub Desktop.

Select an option

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
/**
* 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