/49. Group Anagrams

49. Group Anagrams

Medium
Arrays72.3% acceptance

Given a list of lowercase English strings, group the strings that are anagrams of each other. Return a list of lists, where each sublist contains strings that are anagrams. The order of groups and the order of strings within each group does not matter.

Example 1

Input: [abc, cab, bca, xyz, zyx, yxz, foo]

Output: [[abc, cab, bca], [xyz, zyx, yxz], [foo]]

Explanation: Three groups: anagrams of abc, anagrams of xyz, and foo alone.

Example 2

Input: [mno, onm, nom, pqr, rpq]

Output: [[mno, onm, nom], [pqr, rpq]]

Explanation: Two groups: anagrams of mno, anagrams of pqr.

Constraints

  • 1 <= len(string_list) <= 104
  • 0 <= len(s) <= 100 for each s in string_list
  • Each string in string_list consists of lowercase English letters
Python (current runtime)

Case 1

Input: ['listen', 'silent', 'enlist', 'google', 'gogole', 'abc', 'bac']

Expected: [['listen', 'silent', 'enlist'], ['google', 'gogole'], ['abc', 'bac']]

Case 2

Input: ['a', 'b', 'c', 'a']

Expected: [['a', 'a'], ['b'], ['c']]

Case 3

Input: ['']

Expected: [['']]

Case 4

Input: ['abcd', 'dcba', 'bcda', 'dabc', 'efgh']

Expected: [['abcd', 'dcba', 'bcda', 'dabc'], ['efgh']]