AlgoLounge

Group Anagrams

medium

Leetcode 49. Group Anagrams

Given an array of strings strs, group all anagrams together into sublists. You may return the output in any order.

An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.

Examples

Example 1

Input:  strs = ["act","pots","tops","cat","stop","hat"]
Output: [["hat"],["act", "cat"],["stop", "pots", "tops"]]

Example 2

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

Example 3

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

Constraints

  • 1 <= strs.length <= 1000
  • 0 <= strs[i].length <= 100
  • strs[i] is made up of lowercase English letters
TypeScript
Loading editor…
List of test cases (11)
Case 1
strs = ["act","pots","tops","cat","stop","hat"]
Case 2
strs = ["x"]
Case 3
strs = [""]
Case 4
strs = ["eat","tea","tan","ate","nat","bat"]
Case 5
strs = ["a"]
Case 6
strs = ["ab","ba"]
Case 7
strs = ["abc","def","ghi"]
Case 8
strs = ["listen","silent","elbow","below"]
Case 9
strs = ["aab","aba","baa","abc","acb","bac","bca","cab","cba"]
Case 10
strs = ["race","care","acre","god","dog"]
Case 11
strs = ["zoo","ooz","zzo","abc","bca"]