Challenge - 5 Problems
Anagram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sorting-Based Anagram Check
What is the output of the following JavaScript code that checks if two strings are anagrams by sorting their characters?
DSA Javascript
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const sorted1 = str1.split('').sort().join('');
const sorted2 = str2.split('').sort().join('');
return sorted1 === sorted2;
}
console.log(areAnagrams('listen', 'silent'));
console.log(areAnagrams('hello', 'bello'));Attempts:
2 left
💡 Hint
Think about what sorting the characters in both strings does and how it helps compare them.
✗ Incorrect
Sorting both strings arranges their characters in order. If they are anagrams, the sorted strings will be identical. 'listen' and 'silent' are anagrams, so the first output is true. 'hello' and 'bello' differ in characters, so the second output is false.
❓ Predict Output
intermediateOutput of Frequency Map Anagram Check
What is the output of this JavaScript code that checks if two strings are anagrams using character frequency maps?
DSA Javascript
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const freq = {};
for (const char of str1) {
freq[char] = (freq[char] || 0) + 1;
}
for (const char of str2) {
if (!freq[char]) return false;
freq[char]--;
}
return true;
}
console.log(areAnagrams('triangle', 'integral'));
console.log(areAnagrams('apple', 'papel'));Attempts:
2 left
💡 Hint
Check how the frequency map counts characters and how the second loop reduces counts.
✗ Incorrect
Both 'triangle' and 'integral' have the same characters with the same counts, so true. Similarly, 'apple' and 'papel' are anagrams, so true again.
❓ Predict Output
advancedOutput of Case-Insensitive Anagram Check
What is the output of this JavaScript code that checks if two strings are anagrams ignoring case differences?
DSA Javascript
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const normalize = s => s.toLowerCase().split('').sort().join('');
return normalize(str1) === normalize(str2);
}
console.log(areAnagrams('Dormitory', 'DirtyRoom'));
console.log(areAnagrams('Schoolmaster', 'TheClassroom'));Attempts:
2 left
💡 Hint
Consider how converting to lowercase affects the comparison.
✗ Incorrect
Converting both strings to lowercase before sorting makes the comparison case-insensitive. Both pairs are anagrams ignoring case, so both outputs are true.
❓ Predict Output
advancedOutput of Anagram Check with Unicode Characters
What is the output of this JavaScript code that checks if two strings with Unicode characters are anagrams?
DSA Javascript
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const freq = new Map();
for (const char of str1) {
freq.set(char, (freq.get(char) || 0) + 1);
}
for (const char of str2) {
if (!freq.has(char) || freq.get(char) === 0) return false;
freq.set(char, freq.get(char) - 1);
}
return true;
}
console.log(areAnagrams('résumé', 'sérumé'));
console.log(areAnagrams('café', 'face'));Attempts:
2 left
💡 Hint
Unicode characters are treated as single characters in JavaScript strings.
✗ Incorrect
'résumé' and 'sérumé' have the same characters with the same counts, so true. 'café' and 'face' differ in characters, so false.
🧠 Conceptual
expertTime Complexity of Anagram Check Methods
Which statement correctly describes the time complexity of the two common anagram check methods: sorting-based and frequency map-based?
Attempts:
2 left
💡 Hint
Consider the cost of sorting versus counting characters.
✗ Incorrect
Sorting characters takes O(n log n) time, while building and checking frequency maps takes O(n) time, where n is the string length.
