Ugly Java one-liner


  • 0

    Disclaimer: I just did this to show off :p

    public String frequencySort(String s) {
      return s.chars().mapToObj(c -> String.valueOf((char) c))
              .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).entrySet().stream()
              .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
              .flatMap(e -> Collections.nCopies(e.getValue().intValue(), e.getKey()).stream())
              .collect(Collectors.joining());
    }
    

Log in to reply
 

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