LeetCode-in-Java

3913. Sort Vowels by Frequency

Medium

You are given a string s consisting of lowercase English characters.

Rearrange only the vowels in the string so that they appear in non-increasing order of their frequency.

If multiple vowels have the same frequency, order them by the position of their first occurrence in s.

Return the modified string.

Vowels are 'a', 'e', 'i', 'o', and 'u'.

The frequency of a letter is the number of times it occurs in the string.

Example 1:

Input: s = “leetcode”

Output: “leetcedo”

Explanation:

Example 2:

Input: s = “aeiaaioooa”

Output: “aaaaoooiie”

Explanation:

Example 3:

Input: s = “baeiou”

Output: “baeiou”

Explanation:

Constraints:

Solution

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public String sortVowels(String s) {
        int[] freq = new int[26];
        char[] ch = s.toCharArray();
        for (char c : ch) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                freq[c - 'a']++;
            }
        }
        List<int[]> x = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            if (freq[i] > 0) {
                x.add(new int[] {i, freq[i]});
            }
        }
        x.sort(
                (a, b) ->
                        b[1] - a[1] == 0
                                ? s.indexOf((char) (a[0] + 'a')) - s.indexOf((char) (b[0] + 'a'))
                                : b[1] - a[1]);
        int i = 0;
        for (int[] f : x) {
            while (f[1] > 0) {
                char c = ch[i];
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                    ch[i] = (char) (f[0] + 'a');
                    f[1]--;
                }
                i++;
            }
        }
        return new String(ch);
    }
}