HashMap•Pattern: Canonical Form / HashMap•Medium

Group anagrams together

Problem Intuition & Objective

Cluster strings that are anagrams of each other.

Time ComplexityO(n × k log k) — k = max string length
Optimal
Space ComplexityO(n×k)
Memory Bounds
Optimal Algorithmic Approach

HashMap with sorted-string keys.

Core Invariants & Edge Cases

  • Sort each string to form a canonical key
  • Use the sorted string as a HashMap key; group original strings under it
  • Alternative: character-count tuple as key (faster for long strings)
Previous ProblemWalls and gates — fill distance to nearest gateNext Problem Top K frequent elements

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.