Solve it with java 8 stream pattern


  • 0
    K

    Nothing fancy comparing to python or ruby, but if you prefer java and hate to write a lot of for loops:

        public List<List<String>> groupAnagrams(String[] strs) {
            return Arrays.asList(strs).stream()
                    .collect(Collectors.groupingBy(str -> {
                        char[] patt = str.toCharArray();
                        Arrays.sort(patt);
                        return String.valueOf(patt);
                    }))
                    .entrySet()
                    .stream()
                    .map(Map.Entry::getValue)
                    .collect(Collectors.toList());
        }
    

Log in to reply
 

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