medium1-D Dynamic ProgrammingFacebookAmazonGoogle
Dictionary Breaker
A tokenizer checks whether a run-on string can be chopped into a sequence of dictionary words.
Given a string s and a dictionary of words, return true if s can be segmented into one or more dictionary words.
Choose approach
1
00
1?
2?
3?
4?
5?
6?
7?
8i
1/9
Narrationstep 1 of 9
dp[1] = F (prefix "l")
Variables
i=1
breakable=false
How to think about it
- Reduce to reachability on a string of prefix endpoints.
- Trie preprocessing can speed inner check.
Edge cases to catch in interview
- Empty s -> true.
- No dict word is prefix of s -> false.
Examples
Example 1
true{"s":"applepenapple","wordDict":["apple","pen"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed