Search⌘ K
AI Features

DIY: Group Anagrams

Explore how to group words that are anagrams by rearranging their letters, using C++ algorithms. Learn to implement a function that organizes a list of words into groups of anagrams. This lesson helps you build skills to tackle similar coding interview problems with real-world applications.

Problem Statement

You are given a list of words or phrases, and you need to group the words that are anagrams of each other. An anagram is a word, phrase, or sentence formed from another word by rearranging its letters.

Input

The input will be a list of words or phrases. The following is an example of input:

["word", "sword", "drow", "rowd", "iced", "dice"]

Output

The output should be a list of grouped words. The following is an example output:

[['word', 'drow', 'rowd'], ['sword'], ['iced', 'dice']]

Coding exercise

You need to implement the function groupAnagrams(strs, groups), where strs is the list of words you need to group together, and groups is the output list containing multiple lists of the newly created groups.

C++ 17
void groupAnagrams(std::vector<std::string>& strs, std::vector<std::vector<std::string>>& groups){
// write your code here
}
Group anagrams