↻one more pass.
Arrays & hashing · Medium

Group Anagrams: the idea to remember.

Partition lowercase English words into groups with identical character counts.

Solve on LeetCode ↗

The core insight

Use a frequency signature as the hash map key.

Why it works

Anagrams have the same count in every character bucket, so they map to exactly the same key.

The mistake to avoid

Concatenating counts without separators can create ambiguous signatures.

Time: O(total characters + 26 × number of words) expectedSpace: O(number of words × 26 + output)

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

List<List<String>> groupAnagrams(String[] words) {
    Map<String, List<String>> groups = new HashMap<>();
    for (String word : words) {
        int[] count = new int[26];
        for (char c : word.toCharArray()) count[c - 'a']++;
        // Delimited serialization distinguishes [1,11] from [11,1].
        String key = Arrays.toString(count);
        groups.computeIfAbsent(key, k -> new ArrayList<>()).add(word);
    }
    return new ArrayList<>(groups.values());
}

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.