my easy java solution


  • 0
    Y
    public class Solution {
        public String frequencySort(String s) {
            StringBuilder res = new StringBuilder();
            Map<Character, Integer> map = new HashMap<>();
            for (char c : s.toCharArray()) {
                if (map.containsKey(c))
                    map.put(c, map.get(c) + 1);
                else map.put(c, 1);
            }
            List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
            list.sort((b, a) -> a.getValue().compareTo(b.getValue()));
            for (Map.Entry<Character, Integer> aList : list) {
                for (int j = 0; j < aList.getValue(); j++)
                    res.append(aList.getKey());
            }
            return res.toString();
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.