22. Group Anagrams
22. Group Anagrams
You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:
A large search, messaging, or content classification platform processes massive collections of words such as:
- user search queries
- document keywords
- product titles
- tags and labels
To improve indexing, deduplication, and recommendation quality, the platform needs to group words that are composed of the same characters with the same frequencies, regardless of order.
Each word is stored as a string in an array strs. Words that are anagrams of each other should be grouped together.
This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.
Your Task
Given an array of strings strs, group the anagrams together.
Return a list of groups, where each group contains strings that are anagrams of one another.
The order of groups and the order of strings within each group does not matter.
Input Format
strs: an array of strings
Output Format
- A list of lists of strings, where each inner list contains anagram groups
Examples
Example 1:
strs = ["eat","tea","tan","ate","nat","bat"][["eat","tea","ate"],["tan","nat"],["bat"]]Example 2:
strs = [""][[""]]Example 3:
strs = ["a"][["a"]]Constraints
1 <= strs.length <= 100,0000 <= strs[i].length <= 100Strings consist of lowercase English letters only
Loading editor...
["eat","tea","tan","ate","nat","bat"]
[["eat","tea","ate"],["tan","nat"],["bat"]]
Console Output
Click "Run Code" or "Submit" to see results
Your code will be evaluated by AI with instant feedback