15% off all plans & roadmaps Use code
See plansLeetCode 425 · Solution & editorial
Given a set of words (without duplicates), find all word squares you can build from them.
Word Squares is a Design Patterns problem, in the Tries subpattern. The pattern covers tries, LRU/LFU cache, and data structure design
Implement Trie data structure for prefix-based operations and autocomplete systems.
The reference solution runs in O(n^2 * n!) time and O(n^2) space.
Word Squares has been reported in interviews at Bloomberg.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Word Squares is a Design Patterns problem, in the Tries subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.