15% off all plans & roadmaps Use code
See plansLeetCode 648 · Solution & editorial
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word successor. For example, when the root "an" is followed by the successor word "other", we can form a new word "another".
Replace Words 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) time and O(t) space.
Replace Words has been reported in interviews at Uber.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Replace Words 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.