15% off all plans & roadmaps Use code
See plansLeetCode 745 · Solution & editorial
Design a special dictionary which has some words and allows you to search the words in it by a prefix and a suffix.
Prefix and Suffix Search 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.
Prefix and Suffix Search has been reported in interviews at Amazon, Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Prefix and Suffix Search 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.