📖 Scenario: You work in a word game company. You want to group words that are anagrams of each other to help players find word matches quickly.
🎯 Goal: Build a program that groups a list of words into sets where each set contains words that are anagrams of each other.
📋 What You'll Learn
Create an array of strings with exact words given
Create a helper function to sort characters in a string
Use a hash map (dictionary) to group words by their sorted character key
Print the groups of anagrams
💡 Why This Matters
🌍 Real World
Grouping anagrams helps in word games, search engines, and text analysis to find related words quickly.
💼 Career
Understanding hash maps and string manipulation is essential for software development, especially in data processing and algorithm design.
Progress0 / 4 steps
1
Create the list of words
Create an array of strings called words with these exact entries: "eat", "tea", "tan", "ate", "nat", "bat".
DSA C
Hint
Use char *words[] = {"eat", "tea", "tan", "ate", "nat", "bat"}; to create the array.
2
Create a function to sort characters in a string
Write a function called sortString that takes a char* string and sorts its characters in alphabetical order.
DSA C
Hint
Use nested loops to compare and swap characters to sort the string.
3
Group words by sorted key using a hash map
Create a hash map using arrays to group words by their sorted character key. Use a 2D array groups to store groups and an array groupCounts to count words in each group. For each word, create a copy, sort it using sortString, then check if the sorted key exists in groups. If yes, add the word to that group; if no, create a new group.
DSA C
Hint
Use arrays to simulate a hash map by storing sorted keys and their groups. Use strcpy to copy strings.
4
Print the groups of anagrams
Add code inside main to print each group of anagrams. For each group, print words separated by spaces on one line.
DSA C
Hint
Use nested loops to print each group of words separated by spaces, then print a new line after each group.
Practice
(1/5)
1. What is the main idea behind grouping anagrams using a hash map?
easy
A. Use the length of words as keys to group anagrams
B. Store words in a list without any sorting or grouping
C. Group words by their first character only
D. Use sorted strings as keys to group words with the same letters
Solution
Step 1: Understand anagrams and their properties
Anagrams are words made of the same letters in any order, so sorting letters gives a unique key for each group.
Step 2: Use sorted strings as hash map keys
By sorting each word and using it as a key, all anagrams map to the same key and can be grouped together.
Final Answer:
Use sorted strings as keys to group words with the same letters -> Option D
Quick Check:
Sorted word keys group anagrams [OK]
Hint: Sort each word and use as key to group anagrams [OK]
Common Mistakes:
Grouping by word length only
Using first character as key
Not sorting words before grouping
2. Which of the following is the correct way to declare a hash map (dictionary) in C for grouping anagrams?
easy
A. map anagramGroups;
B. unordered_map anagramGroups;
C. unordered_map anagramGroups;
D. vector<string> anagramGroups;
Solution
Step 1: Identify the data types needed for grouping anagrams
We need a map from a string key (sorted word) to a list of strings (anagrams).
Step 2: Choose the correct C++ STL container
unordered_map fits this need perfectly.
Final Answer:
unordered_map anagramGroups; -> Option B
Quick Check:
Map from string to vector<string> groups anagrams [OK]
Hint: Use unordered_map for grouping anagrams [OK]
Common Mistakes:
Using map which mismatches key and value types
Using vector<string> alone without mapping keys
Using unordered_map which is unrelated
3. What is the output of the following C++ code snippet?
vector<string> words = {"eat", "tea", "tan", "ate", "nat", "bat"};
unordered_map groups;
for (auto &word : words) {
string sortedWord = word;
sort(sortedWord.begin(), sortedWord.end());
groups[sortedWord].push_back(word);
}
for (auto &pair : groups) {
sort(pair.second.begin(), pair.second.end());
for (auto &w : pair.second) cout << w << " ";
cout << endl;
}
medium
A. bat
nat tan
eat tea ate
B. eat tea ate
bat
tan nat
C. tea ate eat
bat
tan nat
D. ate eat tea
bat
nat tan
Solution
Step 1: Group words by sorted keys
Words "eat", "tea", "ate" all sort to "aet" and group together; "tan", "nat" to "ant"; "bat" to "abt".
Step 2: Sort each group and print
Each group is sorted alphabetically before printing: "ate eat tea", "bat", "nat tan".
Final Answer:
bat
nat tan
eat tea ate
-> Option A
Quick Check:
Sorted keys group anagrams; groups sorted before print [OK]
Hint: Sort words and groups before printing for consistent output [OK]
Common Mistakes:
Not sorting groups before printing
Printing groups in wrong order
Confusing sorted keys with original words
4. Identify the bug in this C++ code for grouping anagrams:
vector<string> words = {"abc", "bca", "cab"};
unordered_map groups;
for (auto word : words) {
string key = word;
sort(word.begin(), word.end());
groups[key].push_back(word);
}
medium
A. Sorting is done on 'word' but key uses unsorted string
B. Using unordered_map instead of map causes error
C. Loop variable should be reference to avoid copies
D. Missing initialization of groups map
Solution
Step 1: Check how key and sorted string are assigned
Key is assigned before sorting, so key is unsorted original word.
Step 2: Sorting is done on 'word' but key remains unsorted
Groups use unsorted key, so anagrams won't group correctly.
Final Answer:
Sorting is done on 'word' but key uses unsorted string -> Option A
Quick Check:
Key must be sorted string for correct grouping [OK]
Hint: Sort string before assigning key for hash map [OK]
Common Mistakes:
Assigning key before sorting
Sorting wrong variable
Not using reference in loop (less critical here)
5. Given a list of words, how can you modify the hash map grouping approach to handle case-insensitive anagrams (e.g., "Eat" and "tea" grouped together)?
hard
A. Group words by their first letter ignoring case
B. Use word length as key instead of sorted string
C. Convert all words to lowercase before sorting and grouping
D. Sort words without changing case and group normally
Solution
Step 1: Understand case-insensitive grouping
"Eat" and "tea" differ in case but are anagrams ignoring case.
Step 2: Normalize words by converting to lowercase before sorting
This ensures keys match regardless of original case.
Final Answer:
Convert all words to lowercase before sorting and grouping -> Option C
Quick Check:
Lowercase normalization groups case-insensitive anagrams [OK]
Hint: Lowercase words before sorting to group case-insensitive anagrams [OK]
Common Mistakes:
Grouping by length only
Ignoring case normalization
Sorting without case conversion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.