bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Valid Anagram
āœļøFill the BlankjavascriptEasyArrays & Hashing

Valid Anagram - Fill the Blank

Complete the solution

Problem Brief

Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Puzzle Hints
  1. Read the blank inside this exact statement: "if (s.length !== t.length) return ___;".

  2. Count char frequencies. Increment for s, decrement for t. Negative = mismatch.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 21 companies
AffirmAmazonApple
Valid Anagram — Frequency Count
hashmap
1
5

Input: s="anagram", t="nagaram". Count char frequencies

Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by

Drag tokens into the blanks

1
0
false
true
1function isAnagram(s, t) {
2 if (s.length !== t.length) return ___;
3 const count = {};
4 for (const c of s) count[c] = (count[c] || 0) + ___;
5 for (const c of t) {
6 count[c] = (count[c] || 0) - 1;
7 if (count[c] < ___) return false;
8 }
9 return true;
10}