2dbi

Group Anagrams

viaLeetCode

Problem Group the anagrams in an array of strings; return the groups in any order.

Input / Output

  • Input: string[] strs. Output: list of anagram groups.

Constraints

  • Up to 10^4 strings of length <= 100; O(total chars) achievable.

Example

  • ["eat","tea","tan","ate","nat","bat"] → [["eat","tea","ate"],["tan","nat"],["bat"]].

Expected approach

  • Canonical key per string into a hash map: sorted characters (O(L log L) per string) or, better, a 26-count signature string like "1#0#2#…" (O(L), handles longer strings and avoids sort). Discuss key-collision safety of the delimiter (why "111" is ambiguous without separators) and the prime-product hash idea plus its overflow flaw. O(N·L) with counting keys.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account