Search⌘ K
AI Features

Group Anagrams

Discover how to group strings into anagram clusters by tracking character frequency and rearrangement patterns. This lesson helps you understand what data to track for effective frequency analysis, enhancing your ability to solve common coding problems involving anagrams.

Statement

Given a list of strings strs, group together all strings that are anagrams of each other.

An anagram is a string formed by rearranging the letters of another string, using all original letters exactly once. For example, “eat”, “tea”, and “ate” are anagrams.

Return a list of groups, where each group contains strings that are anagrams of each other.

Note: The order of the groups and the order of strings within each group does not matter.

Constraints:

Let strs be the list of strings given as input to find the anagrams.

  • 11 \leq strs.length 103\leq 10^3
  • 00 \leq strs[i].length 100\leq 100
  • strs[i] consists of lowercase English letters.

Examples

canvasAnimation-image
1 / 2

Understand the problem

Let’s take a moment to make sure you've correctly understood the problem. The quiz below helps you check if you're solving the correct problem:

Group Anagrams

1.

What is the output if the following list is given as input?

[‘bat’, ‘tab’, ‘tan’, ‘at’]

A.

[[‘at’], [‘bat’], [‘tab’], [‘tan’]]

B.

[[‘at’, ‘bat’]], [[‘tab’, tan’]]

C.

[[‘at’, ‘bat’, ‘tab’, tan’]]

D.

[[‘tab’, ‘bat’]], [‘tan’], [‘at’]]


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6
7

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > GroupAnagrams.cpp
vector<vector<string>> GroupAnagrams(vector<string>& strs){
// Replace this placeholder return statement with your code
return {{}};
}
Group Anagrams