Java ✅🤯 - Best Solution in Memory
IntuitionApproach
Initialize a StringBuilder to store the modified string.
Loop through the string and collect all vowels into a list.
Sort the vowel list.
Loop
# Intuition
<!-- Describe your first thoughts on how to solve this problem. --> The goal is to sort only the vowels in a given string, leaving the positions of non-vowel characters unchanged. We first identify and collect all vowels, sort them, and then place them back in their original positions in the string.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize a StringBuilder to store the modified string.
2. Loop through the string and collect all vowels into a list.
3. Sort the vowel list.
4. Loop again through the string and replace the vowels with the sorted vowels in the original order of their occurrence.
5. Return the modified string.

# Complexity
- Time complexity: O(n logn)
<!-- Add your time complexity here, e.g. $$O(n)$$ --> 

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ --> 

# Code
```java []
class Solution {
    public String sortVowels(String s) {
        int l = s.length();
        StringBuilder str = new StringBuilder(s);
        List<Character> charList = new ArrayList<>();
        for(int i=0; i<l; i++){
            char ch = s.charAt(i);
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'  ){
                charList.add(ch);
            }
        }
        Collections.sort(charList);
        for(int i=0; i<l; i++){
            char ch = s.charAt(i);
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U'  ){
                str.setCharAt(i, charList.remove(0));
            }
           
        }
        return str.toString();
    }
}
```
['Java']