Design Gurus Logo
Group Anagrams (Medium)
Go Back

Problem Statement

Given a list of strings, the task is to group the anagrams together.

An anagram is a word or phrase formed by rearranging the letters of another, such as "cinema", formed from "iceman".

Example Generation

Example 1:

  • Input: ["dog", "god", "hello"]
  • Output: [["dog", "god"], ["hello"]]
  • Justification: "dog" and "god" are anagrams, so they are grouped together. "hello" does not have any anagrams in the list, so it is in its own group.

Example 2:

  • Input: ["listen", "silent", "enlist"]
  • Output: [["listen", "silent", "enlist"]]
  • Justification: All three words are anagrams of each other, so they are grouped together.

Example 3:

  • Input: ["abc", "cab", "bca", "xyz", "zxy"]
  • Output: [["abc", "cab", "bca"], ["xyz", "zxy"]]
  • Justification: "abc", "cab", and "bca" are anagrams, as are "xyz" and "zxy".

Constraints:

  • 1 <= strs.length <= 10<sup>4</sup>
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

Try it yourself

Try solving this question here:

Python3
Python3