--- id: 69cfca90e8a0a6d4d6871c4e title: "Challenge 264: Anagram Groups" challengeType: 29 dashedName: challenge-264 --- # --description-- Given an array of words, return a 2d array of the words grouped into anagrams. - Words are anagrams if they contain the same letters in any order. - Each word belongs to exactly one group. - Return order doesn't matter. For example, given `["listen", "silent", "hello", "enlist", "world"]`, return `[["listen", "silent", "enlist"], ["hello"], ["world"]]`. # --hints-- `group_anagrams(["listen", "silent", "hello", "enlist", "world"])` should return `[["listen", "silent", "enlist"], ["hello"], ["world"]]`. ```js ({test: () => { runPython(` from unittest import TestCase groups = group_anagrams(["listen", "silent", "hello", "enlist", "world"]) sorted_groups = sorted([sorted(g) for g in groups], key=lambda g: g[0]) TestCase().assertEqual(sorted_groups, [["enlist", "listen", "silent"], ["hello"], ["world"]])`) }}) ``` `group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])` should return `[["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]`. ```js ({test: () => { runPython(` from unittest import TestCase groups = group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) sorted_groups = sorted([sorted(g) for g in groups], key=lambda g: g[0]) TestCase().assertEqual(sorted_groups, [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]])`) }}) ``` `group_anagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"])` should return `[["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]]`. ```js ({test: () => { runPython(` from unittest import TestCase groups = group_anagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"]) sorted_groups = sorted([sorted(g) for g in groups], key=lambda g: g[0]) TestCase().assertEqual(sorted_groups, [["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]])`) }}) ``` `group_anagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"])` should return `[["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]]`. ```js ({test: () => { runPython(` from unittest import TestCase groups = group_anagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"]) sorted_groups = sorted([sorted(g) for g in groups], key=lambda g: g[0]) TestCase().assertEqual(sorted_groups, [["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]])`) }}) ``` # --seed-- ## --seed-contents-- ```py def group_anagrams(words): return words ``` # --solutions-- ```py def group_anagrams(words): groups = {} for word in words: key = ''.join(sorted(word)) if key not in groups: groups[key] = [] groups[key].append(word) return list(groups.values()) ```