bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Group Anagrams
🧩Arrange CodetypescriptMediumArrays & Hashing

Group Anagrams - Arrange Code (TypeScript)

Put the TypeScript code in order

Problem Brief

Given an array of strings strs, group the anagrams together. You can return the answer in any order. 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. Ignore visual position first; place the line that creates required state before lines that read it. One candidate is "function groupAnagrams(strs: string[]): string[][] {".

  2. Arrange the typescript code lines in the correct order to form a working solution.

  3. When two lines both look plausible, choose the one whose output is needed by the next line.

Asked at 36 companies
AdobeAffirmAmazon
Group Anagrams — Sorted Key HashMap
hashmap
1
8

Input: ["eat","tea","tan","ate","nat","bat"]. Key = sorted chars

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
  • 1function groupAnagrams(strs: string[]): string[][] {
  • 2 const key = s.split('').sort().join('');
  • 3 for (const s of strs) {
  • 4 map.get(key)!.push(s);
  • 5 }
  • 6 return Array.from(map.values());
  • 7 const map = new Map<string, string[]>();
  • 8 if (!map.has(key)) map.set(key, []);
  • 9}