✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(With Sorting)\n1. Initialization: Initialize two strings, t
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(With Sorting)***\n1. **Initialization:** Initialize two strings, `t` to store the resulting string, and `temp` as a temporary storage for vowels.\n\n1. **Extracting Vowels:** Iterate through the original string `s`, and if a character is a vowel, add it to the `temp` string.\n\n1. **Sorting Vowels:** Sort the `temp` string to get the vowels in sorted order.\n\n1. **Reconstructing the Result:** Iterate through the original string s again. If the character is a vowel, add the corresponding character from the sorted `temp` string. If it\'s a non-vowel, add it directly.\n\n1. **Returning Result:** Return the final sorted string `t`.\n\n# Complexity\n- *Time complexity:*\n   $$O(nlogn)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\n\n\nclass Solution {\nprivate:\n    // Helper function to check if a character is a vowel.\n    bool isVowel(char c) {\n        return (c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\' ||\n                c == \'A\' || c == \'E\' || c == \'I\' || c == \'O\' || c == \'U\');\n    }\n\npublic:\n    // Function to sort vowels in a string and maintain the original order of non-vowel characters.\n    string sortVowels(string s) {\n        string t = "";        // Resulting string after sorting vowels.\n        string temp = "";     // Temporary string to store vowels from the original string.\n\n        // Step 1: Extract and store vowels from the original string in temporary string \'temp\'.\n        for (int i = 0; i < s.size(); i++) {\n            if (isVowel(s[i])) {\n                temp += s[i];\n            }\n        }\n\n        // Step 2: Sort the temporary string \'temp\' containing vowels.\n        sort(temp.begin(), temp.end());\n\n        int j = 0;\n        // Step 3: Reconstruct the resulting string \'t\' by inserting sorted vowels and maintaining the order of non-vowel characters.\n        for (int i = 0; i < s.size(); i++) {\n            if (isVowel(s[i])) {\n                t += temp[j];\n                j++;\n            } else {\n                t += s[i];\n            }\n        }\n\n        // Step 4: Return the final sorted string.\n        return t;\n    }\n};\n\n\n\n```\n```C []\n\n// Helper function to check if a character is a vowel.\nint isVowel(char c) {\n    return (c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\' ||\n            c == \'A\' || c == \'E\' || c == \'I\' || c == \'O\' || c == \'U\');\n}\n\n// Function to sort vowels in a string and maintain the original order of non-vowel characters.\nchar* sortVowels(const char* s) {\n    char* t = (char*)malloc(strlen(s) + 1); // Resulting string after sorting vowels.\n    char* temp = (char*)malloc(strlen(s) + 1); // Temporary string to store vowels from the original string.\n\n    // Step 1: Extract and store vowels from the original string in temporary string \'temp\'.\n    int tempIndex = 0;\n    for (int i = 0; i < strlen(s); i++) {\n        if (isVowel(s[i])) {\n            temp[tempIndex++] = s[i];\n        }\n    }\n    temp[tempIndex] = \'\\0\';\n\n    // Step 2: Sort the temporary string \'temp\' containing vowels.\n    for (int i = 0; i < tempIndex - 1; i++) {\n        for (int j = 0; j < tempIndex - i - 1; j++) {\n            if (temp[j] > temp[j + 1]) {\n                // Swap characters if they are out of order.\n                char tempChar = temp[j];\n                temp[j] = temp[j + 1];\n                temp[j + 1] = tempChar;\n            }\n        }\n    }\n\n    int j = 0;\n    // Step 3: Reconstruct the resulting string \'t\' by inserting sorted vowels and maintaining the order of non-vowel characters.\n    for (int i = 0; i < strlen(s); i++) {\n        if (isVowel(s[i])) {\n            t[i] = temp[j++];\n        } else {\n            t[i] = s[i];\n        }\n    }\n    t[strlen(s)] = \'\\0\';\n\n    // Free allocated memory for temporary string.\n    free(temp);\n\n    // Step 4: Return the final sorted string.\n    return t;\n}\n\n\n```\n```Java []\n\npublic class Solution {\n    // Helper function to check if a character is a vowel.\n    private boolean isVowel(char c) {\n        return (c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\' ||\n                c == \'A\' || c == \'E\' || c == \'I\' || c == \'O\' || c == \'U\');\n    }\n\n    // Function to sort vowels in a string and maintain the original order of non-vowel characters.\n    public String sortVowels(String s) {\n        StringBuilder t = new StringBuilder();   // Resulting string after sorting vowels.\n        StringBuilder temp = new StringBuilder(); // Temporary string to store vowels from the original string.\n\n        // Step 1: Extract and store vowels from the original string in the temporary string \'temp\'.\n        for (int i = 0; i < s.length(); i++) {\n            if (isVowel(s.charAt(i))) {\n                temp.append(s.charAt(i));\n            }\n        }\n\n        // Step 2: Sort the temporary string \'temp\' containing vowels.\n        char[] tempArray = temp.toString().toCharArray();\n        Arrays.sort(tempArray);\n\n        // Step 3: Reconstruct the resulting string \'t\' by inserting sorted vowels and maintaining the order of non-vowel characters.\n        int j = 0;\n        for (int i = 0; i < s.length(); i++) {\n            if (isVowel(s.charAt(i))) {\n                t.append(tempArray[j]);\n                j++;\n            } else {\n                t.append(s.charAt(i));\n            }\n        }\n\n        // Step 4: Return the final sorted string.\n        return t.toString();\n    }\n}\n\n\n```\n```python3 []\n\nclass Solution:\n    # Helper function to check if a character is a vowel.\n    def isVowel(self, c):\n        return c.lower() in [\'a\', \'e\', \'i\', \'o\', \'u\']\n\n    # Function to sort vowels in a string and maintain the original order of non-vowel characters.\n    def sortVowels(self, s):\n        t = ""        # Resulting string after sorting vowels.\n        temp = ""     # Temporary string to store vowels from the original string.\n\n        # Step 1: Extract and store vowels from the original string in the temporary string \'temp\'.\n        for char in s:\n            if self.isVowel(char):\n                temp += char\n\n        # Step 2: Sort the temporary string \'temp\' containing vowels.\n        temp = \'\'.join(sorted(temp))\n\n        j = 0\n        # Step 3: Reconstruct the resulting string \'t\' by inserting sorted vowels and maintaining the order of non-vowel characters.\n        for char in s:\n            if self.isVowel(char):\n                t += temp[j]\n                j += 1\n            else:\n                t += char\n\n        # Step 4: Return the final sorted string.\n        return t\n\n\n```\n```javascript []\nclass Solution {\n    // Helper function to check if a character is a vowel.\n    isVowel(c) {\n        return [\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\'].includes(c);\n    }\n\n    // Function to sort vowels in a string and maintain the original order of non-vowel characters.\n    sortVowels(s) {\n        let t = "";        // Resulting string after sorting vowels.\n        let temp = "";     // Temporary string to store vowels from the original string.\n\n        // Step 1: Extract and store vowels from the original string in the temporary string \'temp\'.\n        for (let char of s) {\n            if (this.isVowel(char)) {\n                temp += char;\n            }\n        }\n\n        // Step 2: Sort the temporary string \'temp\' containing vowels.\n        temp = temp.split(\'\').sort().join(\'\');\n\n        let j = 0;\n        // Step 3: Reconstruct the resulting string \'t\' by inserting sorted vowels and maintaining the order of non-vowel characters.\n        for (let char of s) {\n            if (this.isVowel(char)) {\n                t += temp[j];\n                j++;\n            } else {\n                t += char;\n            }\n        }\n\n        // Step 4: Return the final sorted string.\n        return t;\n    }\n}\n\n\n```\n\n---\n\n#### ***Approach 2(Count Sorting)***\n1. **Function isVowel:**\n\n    - This function checks if a given character is a vowel.\n    - It returns `true` if the character is `\'a\', \'e\', \'o\', \'u\', \'i\'` (both lowercase and uppercase), and `false` otherwise.\n1. **Function sortVowels:**\n\n    - Initialize an unordered map `count` to store the frequencies of each vowel in the input string `s`.\n    - Iterate through each character in the input string `s`:\n        - If the character is a vowel, increment its count in the `count` map.\n    - Create a string `sortedVowel` containing all vowels in uppercase followed by vowels in lowercase.\n    - Initialize an empty string `ans` to store the final sorted string.\n    - Initialize an index `j` for iterating through the sorted vowels.\n    - Iterate through each character in the input string `s`:\n        - If the character is not a vowel, append it to the result string `ans`.\n        - If the character is a vowel, find the next available vowel with remaining count in the `count` map, and append it to the result string `ans`. Decrement its count in the map.\n    - Return the final sorted string `ans`.\n1. **Overall:**\n\n    - The code effectively sorts the vowels in the input string while maintaining the original order of non-vowel characters.\n    - It uses an unordered map to count the occurrences of each vowel and iterates through the string to construct the sorted string.\n    - The sorting is done by selecting vowels from the predefined order of vowels (`sortedVowel`), and the remaining count is tracked to ensure each vowel is used the correct number of times.\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Returns true if the character is a vowel.\n    bool isVowel(char c) {\n        return c == \'a\' || c == \'e\' || c == \'o\'|| c == \'u\'|| c == \'i\' \n            || c == \'A\' || c == \'E\' || c == \'O\'|| c == \'U\'|| c == \'I\';\n    }\n    \n    string sortVowels(string s) {\n        unordered_map<char, int> count;\n\n        // Store the frequencies for each character.\n        for (char c : s) {\n            if (isVowel(c)) {\n                count[c]++;\n            }\n        }\n\n        // Sorted string having all the vowels.\n        string sortedVowel = "AEIOUaeiou";\n        string ans;\n        int j = 0;\n        for (int i = 0; i < s.size(); i++) {\n            if (!isVowel(s[i])) {\n                ans += s[i];\n            } else {\n                // Skip to the character which is having remaining count.\n                while (count[sortedVowel[j]] == 0) {\n                    j++;\n                }\n\n                ans += sortedVowel[j];\n                count[sortedVowel[j]]--;\n            }\n        }\n        return ans;\n    }\n};\n\n\n```\n```C []\n\n\n// Function to check if a character is a vowel.\nint isVowel(char c) {\n    return (c == \'a\' || c == \'e\' || c == \'o\' || c == \'u\' || c == \'i\' ||\n            c == \'A\' || c == \'E\' || c == \'O\' || c == \'U\' || c == \'I\');\n}\n\n// Function to sort vowels in a string and maintain the original order of non-vowel characters.\nchar* sortVowels(char* s) {\n    // Store the frequencies for each character.\n    int count[256] = {0}; // Assuming ASCII characters\n\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (isVowel(s[i])) {\n            count[s[i]]++;\n        }\n    }\n\n    // Sorted string having all the vowels.\n    char sortedVowel[] = "AEIOUaeiou";\n    char* ans = (char*)malloc((strlen(s) + 1) * sizeof(char));\n    int j = 0;\n\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (!isVowel(s[i])) {\n            ans[i] = s[i];\n        } else {\n            // Skip to the character which is having remaining count.\n            while (count[sortedVowel[j]] == 0) {\n                j++;\n            }\n\n            ans[i] = sortedVowel[j];\n            count[sortedVowel[j]]--;\n        }\n    }\n\n    ans[strlen(s)] = \'\\0\'; // Null-terminate the result string\n    return ans;\n}\n\n\n```\n```Java []\nclass Solution {\n    // Returns true if the character is a vowel.\n    private boolean isVowel(char c) {\n        return c == \'a\' || c == \'e\' || c == \'o\'|| c == \'u\'|| c == \'i\' \n            || c == \'A\' || c == \'E\' || c == \'O\'|| c == \'U\'|| c == \'I\';\n    }\n    \n    public String sortVowels(String s) {\n        Map<Character, Integer> count = new HashMap<>();\n\n        // Store the frequencies for each character.\n        for (char c : s.toCharArray()) {\n            if (isVowel(c)) {\n                count.put(c, count.getOrDefault(c, 0) + 1);\n            }\n        }\n\n        // Sorted string having all the vowels.\n        String sortedVowel = "AEIOUaeiou";\n        StringBuilder ans = new StringBuilder();\n        int j = 0;\n        for (char c : s.toCharArray()) {\n            if (!isVowel(c)) {\n                ans.append(c);\n            } else {\n                // Skip to the character which is having remaining count.\n                while (count.get(sortedVowel.charAt(j)) == 0) {\n                    j++;\n                }\n\n                ans.append(sortedVowel.charAt(j));\n                count.put(sortedVowel.charAt(j), count.get(sortedVowel.charAt(j)) - 1);\n            }\n        }\n        return ans.toString();\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    # Returns true if the character is a vowel.\n    def isVowel(self, c: str) -> bool:\n        return c in \'aeiouAEIOU\'\n    \n    def sortVowels(self, s: str) -> str:\n        count = {}\n\n        # Store the frequencies for each character.\n        for c in s:\n            if self.isVowel(c):\n                count[c] = count.get(c, 0) + 1\n\n        # Sorted string having all the vowels.\n        sortedVowel = "AEIOUaeiou"\n        ans = []\n        j = 0\n        for c in s:\n            if not self.isVowel(c):\n                ans.append(c)\n            else:\n                # Skip to the character which is having remaining count.\n                while count[sortedVowel[j]] == 0:\n                    j += 1\n\n                ans.append(sortedVowel[j])\n                count[sortedVowel[j]] -= 1\n\n        return \'\'.join(ans)\n\n\n\n```\n```javascript []\nclass Solution {\n    // Returns true if the character is a vowel.\n    isVowel(c) {\n        return \'aeiouAEIOU\'.includes(c);\n    }\n\n    sortVowels(s) {\n        const count = {};\n\n        // Store the frequencies for each character.\n        for (const c of s) {\n            if (this.isVowel(c)) {\n                count[c] = (count[c] || 0) + 1;\n            }\n        }\n\n        // Sorted string having all the vowels.\n        const sortedVowel = "AEIOUaeiou";\n        let ans = \'\';\n        let j = 0;\n        for (const c of s) {\n            if (!this.isVowel(c)) {\n                ans += c;\n            } else {\n                // Skip to the character which is having remaining count.\n                while (count[sortedVowel[j]] === 0) {\n                    j++;\n                }\n\n                ans += sortedVowel[j];\n                count[sortedVowel[j]]--;\n            }\n        }\n        return ans;\n    }\n}\n\n\n```\n\n---\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['String', 'C', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript']