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

Starter: count one character

Increase the count for the current letter. Goal: When the same letter appears again, add one to its saved count.

Starter step 11 of 20Work with strings

Treat a string as a sequence of characters.

One loop adds counts; the next loop subtracts them.

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: "counts.a ___ 1;".

  2. A frequency count increases when you see the same character again. The correct token works because it preserves the goal: When the same letter appears again, add one to its saved count.

  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

null
+=
1// Goal: When the same letter appears again, add one to its saved count.
2function starterExample() {
3 const counts = { a: 1 };
4 counts.a ___ 1;
5}