15% off all plans & roadmaps Use code
See plansProblem editorial
Given a string s and a collection of distinct words in dictionary, split s into consecutive, non-overlapping pieces. A piece that exactly matches a word in dictionary contributes no extra characters; every other character contributes one extra character. Dictionary words may be used wherever they match, including more than once.
Extra Characters in a String is a Design Patterns problem, in the Tries subpattern. The pattern covers tries, LRU/LFU cache, and data structure design
The reference solution runs in O(n * d * L), where n is the length of s, d is the number of dictionary words, and L is the maximum dictionary-word length. time and O(n) auxiliary space. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Extra Characters in a String 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.