Short Java solution


  • 0
    M
    public String frequencySort(String s) {
        StringBuilder sb = new StringBuilder();
        s.chars().mapToObj(c -> (char)c)
            .collect(Collectors.toMap(c -> c, c -> 1, (c1, c2) -> c1 + c2))
            .entrySet().stream().sorted((e1, e2) -> e2.getValue() - e1.getValue())
            .forEach(e -> 
        {
            for(int i = 0; i < e.getValue(); i++)
                sb.append(e.getKey());
        });
        return sb.toString();
    }

Log in to reply
 

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