Group Anagrams Java | Fresher Interview
Fast recall
Quick revision
Core answer
O(L log L) per word) or a 26-count signature (O(L) per word), then bucket words into a HashMap> . Total O(n*L) with the count-key. The map's values are the anagram groups.Whether you map each word to a canonical anagram key and group with a HashMap of key -> list
Comparing every pair of words (O(n^2 * L)) instead of keying, or a count-array key without a stable string form
Offer the O(L) count-based key (e.g. '#1#0#2...') as faster than sorting each word
Interview-ready guide
Interview answer
The core idea for grouping anagrams is to give every word a canonical key that's the same for all words that are anagrams of each other, then bucket the words by that key in a hash map from key to a list of words.
Anagrams are words with the same letters in different orders, like eat, tea, and ate. There are two natural ways to build the canonical key. The first is to sort the characters of each word; eat, tea, and ate all sort to a-e-t, so that sorted string becomes the key.
Sorting each word is O of L log L, where L is the word length. The second, slightly faster, way is a count-based key. I build a frequency array of the twenty-six letters for the word, then turn that array into a string, for example by joining the counts with separators.
Two anagrams produce the same count array and therefore the same string key, and building it is O of L per word, avoiding the sort. With n words, the total time is O of n times L using the count key. The alternative of comparing every pair of words to see if they're anagrams would be O of n squared times L, so the keying approach is a big win.
A couple of Java specifics: I can't use the int array directly as a map key because arrays compare by identity, so I convert it to a string; and I use computeIfAbsent to create each bucket's list on demand. At the end, the map's values are exactly the groups of anagrams.
Group Anagrams Java | Fresher Interview
Direct Answer
Group by a canonical key: two words are anagrams iff they share the same key. Build the key by either sorting each word's letters (O(L log L) per word) or a 26-count signature (O(L) per word), then bucket words into a HashMap<key, List<String>>. Total O(n*L) with the count-key. The map's values are the anagram groups.
Deep Dive & Architecture
Anagrams share the same characters with the same counts. Convert every word into a canonical signature, then use that signature as a map key. Words with the same key naturally collect in the same list.
Worked example: [eat, tea, tan, ate, nat, bat]
Sorting the letters gives signature aet for eat, tea, and ate; ant for tan and nat; and abt for bat. The map therefore forms the groups [eat, tea, ate], [tan, nat], and [bat].
For lowercase English letters, a 26-value frequency signature avoids sorting. It might encode a1#e1#t1 for the first group. Separators or fixed positions are necessary so counts such as 1, 11 cannot be confused with 11, 1. For general Unicode text, sorted code points or a map-based signature is safer.
If each word has length L, sorting signatures costs about O(n × L log L). Fixed-alphabet counting costs O(n × L). Both need output space plus a map of signatures.
State whether matching ignores case, spaces, or punctuation. The usual coding problem treats the strings exactly as supplied, so normalization should not be added silently.
Code Example
List<List<String>> groupAnagrams(String[] words) {
Map<String, List<String>> groups = new HashMap<>();
for (String w : words) {
int[] count = new int[26];
for (char c : w.toCharArray()) count[c - 'a']++;
StringBuilder key = new StringBuilder(); // O(L) count key
for (int n : count) key.append('#').append(n);
groups.computeIfAbsent(key.toString(), k -> new ArrayList<>()).add(w);
}
return new ArrayList<>(groups.values());
}
// ["eat","tea","tan","ate"]
// eat,tea,ate -> same count key -> one group; tan -> its own group
// result: [[eat,tea,ate],[tan]]
Common Mistakes & Pitfalls
- Pairwise comparison: O(n^2 * L) instead of O(n*L) with keys.
- Using int[] as a map key directly: arrays use identity equality - convert to a String.
- Sorting when not needed: the count key is O(L) vs O(L log L) for sorting.
- Forgetting
computeIfAbsent: avoids manual null-check-then-create.
Check your understanding
Answer these without looking back at the article.
- What is the frequency counting pattern and when do you apply it?
- How do you find the top K most frequent elements in an array?
- How do you find the first non-repeating character in a string?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.