@@ -0,0 +1,1124 @@
+package buildingblock.sorting.bucket.lc451_sortcharactersbyfrequency;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * Given a string, sort it in decreasing order based on the frequency of characters.%0A */%0Apublic class Solution %7B%0A%0A    // Use bucket sort as we did for Top K Frequent Elements%0A    public String frequencySort(String s) %7B%0A        Map%3CCharacter,Integer%3E freq = new HashMap%3C%3E();%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            freq.put(s.charAt(i), freq.getOrDefault(s.charAt(i), 0) + 1);%0A        %7D%0A%0A        StringBuilder%5B%5D bucket = new StringBuilder%5Bs.length() + 1%5D;%0A        for (Map.Entry%3CCharacter,Integer%3E e : freq.entrySet()) %7B%0A            if (bucket%5Be.getValue()%5D == null) %7B%0A                bucket%5Be.getValue()%5D = new StringBuilder();%0A            %7D%0A            for (int i = 0; i %3C e.getValue(); i++) %7B%0A                bucket%5Be.getValue()%5D.append(e.getKey());%0A            %7D%0A        %7D%0A%0A        StringBuilder ret = new StringBuilder();%0A        for (int i = s.length(); i %3E 0; i--) %7B%0A            if (bucket%5Bi%5D != null) ret.append(bucket%5Bi%5D);%0A        %7D%0A        return ret.toString();%0A    %7D%0A%0A%7D%0A

sort characters by frequency