Given an array of strings strs, group the anagrams together. You can return the answer in any order.
strs = ["eat","tea","tan","ate","nat","bat"][["bat"],["nat","tan"],["ate","eat","tea"]]Map<String, List<String>>keymap.computeIfAbsent(key).add(word) — bucket by keynew ArrayList(map.values())Sorting a word gives its canonical form — all anagrams produce the identical sorted string. Using that as a HashMap key buckets every anagram together in a single pass, without comparing every pair of words.