Java ✅ || Easy Solution ✅ || Runtime - 99% beats 💥 || Memory - 97% beats 💥 || Fully Explained ⚠️
Approach\n\nThe approach used in the given solution is as follows:\n1. Iterate through the input string to count the number of vowels, which helps to create a s
# Approach\n\nThe approach used in the given solution is as follows:\n1. Iterate through the input string to count the number of vowels, which helps to create a separate array for storing the vowels.\n2. Create a new char array to store the vowels found in the input string.\n3. Sort the vowel array in non-decreasing order based on their ASCII values.\n4. Iterate through the input string again and replace each vowel with the corresponding sorted vowel from the vowel array.\n5. Convert the modified char array back to a string and return it as the output.\n\nLogic:\n\nThe given solution iterates through the input string twice: once to count the number of vowels and then to sort and replace the vowels. The counting of vowels allows us to create a separate array of the correct size to store the vowels, making it easier to sort them.\n\nIn the first loop, the code counts the number of vowels by checking each character using the "isVowel" helper function. It increments the "cntVowels" variable whenever a vowel is encountered.\n\nNext, the code creates a new character array called "vowels" with the size equal to the number of vowels counted. This array is used to store the vowels found in the input string.\n\nIn the second loop, the code iterates through the input string again and stores each vowel in the "vowels" array. The variable "k" keeps track of the current index in the "vowels" array. Whenever a vowel is encountered in the input string, it is added to the "vowels" array at the current index "k," and "k" is incremented to point to the next empty slot in the "vowels" array.\n\nAfter storing all the vowels in the "vowels" array, the code proceeds to sort the "vowels" array in non-decreasing order using `Arrays.sort`.\n\nFinally, the code iterates through the input string one last time. For each vowel encountered, it replaces it with the corresponding vowel from the sorted "vowels" array using the index "k." The variable "k" ensures that each vowel is replaced with the next available sorted vowel.\n\nIn the end, the modified char array is converted back to a string using `String.valueOf(arr)` and returned as the output.\n\nOverall, the solution effectively sorts the vowels in the input string while keeping the consonants in their original positions, satisfying the requirements of the problem. The time complexity of this solution is O(n log n) due to the sorting step, where n is the length of the input string. The space complexity is O(n) to store the vowel array.\n\n# Complexity\n- Time complexity: $$O(n log n)$$ \n\n- Space complexity: $$O(n)$$ \n\n# Code\n```\nclass Solution {\n    public String sortVowels(String s) {\n        char[] arr = s.toCharArray();\n        int cntVowels = 0;\n\n        for (char ch : arr)\n            if (isVowel(ch))\n                cntVowels++;\n\n        char[] vowels = new char[cntVowels];\n        int k = 0;\n\n        for (char c : arr)\n            if (isVowel(c))\n                vowels[k++] = c;\n\n        Arrays.sort(vowels);\n        k = 0;\n\n        for (int i = 0; i < arr.length; i++)\n            if (isVowel(arr[i])) \n                arr[i] = vowels[k++];\n\n        return String.valueOf(arr);\n    }\n\n    public boolean isVowel(char c) {\n        return c == \'A\' || c == \'E\' || c == \'I\' || c == \'O\' || c == \'U\' || c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\';\n    }\n}\n```
['String', 'Sorting', 'Java', 'C#']