Back to Practice Dashboard
Top 150 InterviewEasy
Group Anagrams
Learn how to solve the 'Group Anagrams' problem. This detailed resource details brute force and optimized approaches.
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?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.