AlgoLounge

Group Anagrams

medium

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Examples

Example 1

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

There is no string in strs that can be rearranged to form "bat". The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.

Example 2

Input:  strs = [""]
Output: [[""]]

Example 3

Input:  strs = ["a"]
Output: [["a"]]

Constraints

  • 1 <= strs.length <= 10⁴
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.
TypeScript
Loading editor…
List of test cases (10)
Case 1
strs = ["eat","tea","tan","ate","nat","bat"]
Case 2
strs = [""]
Case 3
strs = ["a"]
Case 4
strs = ["abc","bca","cab","xyz","zyx","yxz"]
Case 5
strs = ["listen","silent","enlist","google","gooegl"]
Case 6
strs = ["hello","world","olleh","dlrow","test"]
Case 7
strs = ["","","a","a","b"]
Case 8
strs = ["ab","ba","abc","cba","bca","cab","d"]
Case 9
strs = ["ddddddddddg","dgggggggggg"]
Case 10
strs = ["rat","tar","art","car","arc","cra","bat","tab","abt"]