Top 150 InterviewEasy

Group Anagrams

Detailed guide and Python implementation for the 'Group Anagrams' problem.

Problem Statement

Easy

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

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.

Write a function groupAnagrams(strs: List[str]) -> List[List[str]].

Constraints
  • 1 <= len(strs) <= 10^4
  • 0 <= len(strs[i]) <= 100
  • strs[i] consists of lowercase English letters

Examples

Example 1
Input
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output
[["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
Explanation

"eat", "tea", and "ate" are anagrams of each other. "tan" and "nat" are anagrams. "bat" has no anagram in the list.

Example 2
Input
strs = [""]
Output
[[""]]
Explanation

A single empty string forms its own group.

Example 3
Input
strs = ["a"]
Output
[["a"]]
Explanation

A single character string forms its own group.

Need a Hint?
Consider using Arrays & Hashing-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.