## Problem https://leetcode.com/problems/sort-vowels-in-a-string/description/ <br> :::spoiler **Optimal Space & Time Complexity** ``` - Time complexity: O(nlogn) where n is string.length because of sorting - Space complexity: O(n) where n is string.length ``` ::: <br> <hr/> ## Solutions :::spoiler 東 ```javascript= ``` ::: <br> :::spoiler Hao ```javascript= ``` ::: <br> :::spoiler YC ```javascript= ``` ::: <br> :::spoiler SOL ```javascript= ``` ::: --- ## Supplement / Discussion ### 東 ### Hao [Explaination](https://leetcode.com/problems/sort-vowels-in-a-string/solutions/3803223/video-sorting-vowels-in-a-python-string-a-leetcode-challenge-solved/) Basically there are several steps: 1. Extracting the vowels - Checking if a char is vowel - **string.prototype.indexOf()**: 'aeiouAEIOU'.indexOf(char) !== -1 - VOWELS.includes(char): besides declare an array, `new Set(['A', 'E', ...])` could also be employed 2. Sorting vowels - string.prototype.charCodeAt(0) 3. Replacing the original vowels with sorted one - Notice that you could check the index of vowel via - checking if it is a vowel again ```javascript= s.split("") .map((char) => { if (VOWELS.includes(char)) return sortedVowels.pop(); else return char; }) .join(""); ``` - use the recorded vowel indexs to iterate ```javascript= const vowelIndexs = [...]; const newChars = s.split(""); for (let i = 0; i < vowelIndexs.length; i += 1) { newChars[vowelIndexs[i]] = sortedVowels.pop(); } return newChars.join(""); ``` - DON'T: iterate recorded vowel indexs again when iterating the string which leads to time complexity as O(n^2) ```javascript= s.split('').map((char, i) => { if (vowelIdxs.includes(i)) return descSortedVowels.pop(); else return char; }).join(''); ``` ### YC ### SOL