Group Anagrams Using Hash Map in DSA C - Time & Space Complexity
We want to understand how the time needed to group anagrams grows as the list of words gets bigger.
Specifically, how does the code handle checking and grouping words that are anagrams?
Analyze the time complexity of the following code snippet.
void groupAnagrams(char* strs[], int strsSize) {
// Create a hash map to group anagrams
for (int i = 0; i < strsSize; i++) {
char key[100];
strcpy(key, strs[i]);
qsort(key, strlen(key), sizeof(char), cmpChar); // sort characters in the string
hashmapInsert(key, strs[i]);
}
// Output groups from hashmap
}
This code groups words by sorting each word's letters and using the sorted word as a key in a hash map.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting each word's characters inside the loop.
- How many times: Once for each word in the list (strsSize times).
As the number of words grows, the code sorts each word, so the time grows with both the number of words and their length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 words | 10 × sorting each word (depends on word length) |
| 100 words | 100 × sorting each word |
| 1000 words | 1000 × sorting each word |
Pattern observation: The total work grows linearly with the number of words, but sorting each word adds extra cost depending on word length.
Time Complexity: O(n * k log k)
This means the time grows with the number of words (n) times the cost to sort each word of length k.
[X] Wrong: "Sorting all words is just O(n) because we do it once per word."
[OK] Correct: Sorting each word takes time depending on its length (k log k), so total time depends on both number of words and word length.
Understanding how sorting inside loops affects time helps you explain your solution clearly and shows you know how to analyze code performance.
"What if we used counting sort for each word instead of general sorting? How would the time complexity change?"
Practice
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 DQuick Check:
Sorted word keys group anagrams [OK]
- Grouping by word length only
- Using first character as key
- Not sorting words before grouping
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 BQuick Check:
Map from string to vector<string> groups anagrams [OK]
- Using map which mismatches key and value types
- Using vector<string> alone without mapping keys
- Using unordered_map which is unrelated
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 AQuick Check:
Sorted keys group anagrams; groups sorted before print [OK]
- Not sorting groups before printing
- Printing groups in wrong order
- Confusing sorted keys with original words
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 AQuick Check:
Key must be sorted string for correct grouping [OK]
- Assigning key before sorting
- Sorting wrong variable
- Not using reference in loop (less critical here)
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 CQuick Check:
Lowercase normalization groups case-insensitive anagrams [OK]
- Grouping by length only
- Ignoring case normalization
- Sorting without case conversion
Ready to prove it?
Pass the test and this pattern moves unknown
