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

Valid Anagram - Fill the Blank (TypeScript)

Complete the TypeScript 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. Fill in the key parts of the typescript solution. Each blank represents a critical piece of the algorithm.

  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 TypeScriptref

TypeScript adds type safety to JavaScript Map<K,V> and Set<T>. Generic parameters enforce key/value types at compile time. The API is identical to JavaScript but with stricter typing.

-Map<string, number> — typed key-value pairs
-Set<number> — typed unique values
-map.get(key) returns V | undefined
-Use Map over Record<> when keys are dynamic
const map = new Map<string, number>();
map.set('a', 1);
const val: number | undefined = map.get('a');

const set = new Set<number>([1, 2, 3]);
set.has(2);  // true (type-safe)
Official docs →
Map & Set in TypeScriptref

TypeScript adds type safety to JavaScript Map<K,V> and Set<T>. Generic parameters enforce key/value types at compile time. The API is identical to JavaScript but with stricter typing.

-Map<string, number> — typed key-value pairs
-Set<number> — typed unique values
-map.get(key) returns V | undefined
-Use Map over Record<> when keys are dynamic
const map = new Map<string, number>();
map.set('a', 1);
const val: number | undefined = map.get('a');

const set = new Set<number>([1, 2, 3]);
set.has(2);  // true (type-safe)
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

<
0
split
true
null
1
false
1function isAnagram(s: string, t: string) {
2 if (s.length !== t.length) return ___;
3 let first: Array___string | null> = s.split('');
4 const second = t.___('');
5 for (let i = ___; i < second.length; i++) {
6 const element = second[i];
7 let found = first.indexOf(element);
8 if (found !== -1) {
9 first[found] = null;
10 } else {
11 return false;
12 }
13 }
14 return true;
15}