Challenge - 5 Problems
Anagram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Grouping Anagrams by Sorted Key
What is the output of the following TypeScript code that groups anagrams using a hash map with sorted strings as keys?
DSA Typescript
function groupAnagrams(strs: string[]): string[][] {
const map = new Map<string, string[]>();
for (const s of strs) {
const key = s.split('').sort().join('');
if (!map.has(key)) {
map.set(key, []);
}
map.get(key)!.push(s);
}
return Array.from(map.values());
}
const input = ["eat", "tea", "tan", "ate", "nat", "bat"];
console.log(groupAnagrams(input));Attempts:
2 left
💡 Hint
Think about how sorting each string helps group anagrams together as keys in the map.
✗ Incorrect
The code sorts each string to create a key. Anagrams have the same sorted key, so they are grouped together in the map. The output is an array of arrays where each inner array contains anagrams.
🧠 Conceptual
intermediateWhy Use Sorted Strings as Keys in Grouping Anagrams?
Why do we use the sorted version of each string as the key in the hash map when grouping anagrams?
Attempts:
2 left
💡 Hint
Think about what property all anagrams share when their letters are sorted.
✗ Incorrect
All anagrams have the same letters in different orders. Sorting the letters makes all anagrams identical strings, which can be used as unique keys to group them.
🔧 Debug
advancedIdentify the Error in Grouping Anagrams Code
What error will this TypeScript code produce when grouping anagrams?
DSA Typescript
function groupAnagrams(strs: string[]): string[][] {
const map = new Map<string, string[]>();
for (const s of strs) {
const key = s.split('').sort();
if (!map.has(key)) {
map.set(key, []);
}
map.get(key)!.push(s);
}
return Array.from(map.values());
}
const input = ["eat", "tea", "tan"];
console.log(groupAnagrams(input));Attempts:
2 left
💡 Hint
Consider what type is used as the key in the Map and if it is valid.
✗ Incorrect
The code uses an array (result of split and sort) as a key in the Map. JavaScript Maps use object identity for keys, so different arrays with same contents are different keys, causing incorrect grouping.
❓ Predict Output
advancedOutput of Group Anagrams Using Character Count as Key
What is the output of this TypeScript code that groups anagrams using character count as the key?
DSA Typescript
function groupAnagrams(strs: string[]): string[][] {
const map = new Map<string, string[]>();
for (const s of strs) {
const count = new Array(26).fill(0);
for (const char of s) {
count[char.charCodeAt(0) - 97]++;
}
const key = count.join('#');
if (!map.has(key)) {
map.set(key, []);
}
map.get(key)!.push(s);
}
return Array.from(map.values());
}
const input = ["abc", "bca", "cab", "bac", "acb", "cba"];
console.log(groupAnagrams(input));Attempts:
2 left
💡 Hint
All input strings are anagrams of each other, so they should be grouped together.
✗ Incorrect
The character count array creates a unique key for all anagrams. Since all input strings have the same characters, they all map to the same key and are grouped together.
🧠 Conceptual
expertWhy is Character Count Key More Efficient than Sorting for Grouping Anagrams?
Why might using a character count array as a key be more efficient than sorting each string when grouping anagrams?
Attempts:
2 left
💡 Hint
Think about the time complexity of sorting vs counting characters.
✗ Incorrect
Sorting each string takes O(n log n) time, while counting characters takes O(n). For long strings, counting characters is faster and produces a fixed-size key.
