Blind 75
4 / 75Free practice

Group Anagrams

Medium · Arrays & hashing

Jump to code

Partition a list of lowercase English words so each group contains words with identical letter counts. Return the groups and words in any order.

Examples

Example 1

strs = ["eat","tea","tan","ate","nat","bat"]

Output: [["ate","eat","tea"],["bat"],["nat","tan"]]

Example 2

strs = [""]

Output: [[""]]

Constraints

  • 1 <= strs.length <= 1000.
  • 0 <= strs[i].length <= 100
  • strs[i] is made up of lowercase English letters.
Need a hint?

Find a signature that every rearrangement of a word shares.

Asked in real interviews

Candidates reported versions of Group Anagrams from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 10 cases. No account or XP needed for this problem.
Language
Case 1
Input
[
  [
    "eat",
    "tea",
    "tan",
    "ate",
    "nat",
    "bat"
  ]
]
Expected
[
  [
    "ate",
    "eat",
    "tea"
  ],
  [
    "bat"
  ],
  [
    "nat",
    "tan"
  ]
]
Case 2
Input
[
  [
    ""
  ]
]
Expected
[
  [
    ""
  ]
]
Case 3
Input
[
  [
    "a"
  ]
]
Expected
[
  [
    "a"
  ]
]
Case 4
Input
[
  [
    "",
    "b",
    ""
  ]
]
Expected
[
  [
    "",
    ""
  ],
  [
    "b"
  ]
]
Case 5
Input
[
  [
    "abc",
    "bca",
    "cab",
    "xyz",
    "zyx",
    "a"
  ]
]
Expected
[
  [
    "a"
  ],
  [
    "abc",
    "bca",
    "cab"
  ],
  [
    "xyz",
    "zyx"
  ]
]

Drafts are saved per language in this browser when storage is available.