Given a string s and a list of words wordDict, return true if s can be cut into pieces that are each a word from the list, with no letter left over. Any word can be used more than once.
Examples
Example 1:
Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Example 2:
Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: "apple" is used twice; reuse is allowed.
Example 3:
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false
Explanation: every start ("cats"/"cat") strands a middle no word covers.
Example 4:
Input: s = "cars", wordDict = ["car","ca","rs"]
Output: true
Explanation: "ca" + "rs" — the greedy first choice "car" dead-ends.
Constraints
1 <= s.length <= 300
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 20
s and every word consist of lowercase English letters; the words are distinct.
Prerequisites
Dynamic programming — being able to say, in one sentence, what a single cell of your table MEANS. Everything else follows from that sentence. Rolling variables: when a state only looks back one or two steps, the table collapses to a couple of numbers.
How to think about it
1. Mark Each Spot Where a Word Ends Optimal
Intuition
Number the spots before, between and after the letters of s, from 0 to n, its length. A split is a chain of words from spot 0 to spot n, each starting at the spot where the one before it ended. So a spot can be reached when some word ends there and that word starts at a spot already reached. Mark spot 0 first, then walk the end spots in order, and at each one try every word of the list as the piece that ends there: it starts at the end minus its length, and it fits when that start is inside s, is marked, and the letters from it to the end are the word. The first word that fits marks the end, and the answer is whether the last spot is marked. Taking the longest word first is not that: on "carsand" with car, cars and sand, it takes cars and is left with "and", which is no word, and says false, where car, then sand, is a split. Marking every spot keeps both choices: spots 3 and 4 are both marked, and only 3 leads to the end. Mark each spot where a word ends, if that word starts at a spot already marked.
Algorithm
1. Keep a mark for every spot from 0 to n, the length of s, and mark spot 0: nothing has to be cut before it. 2. For every end spot from 1 to n, try each word of the list as the piece that ends there: it would start at end minus its length. 3. If that start is inside s, is marked, and the letters from start to end are the word, mark the end and stop trying words. 4. After the last end spot, return whether spot n is marked.
Time & Space
Time O(n · m · k): each of the n spots tries each of the m words and compares up to k letters, the longest word's. Space O(n): one mark for every spot.
2. Reachable Prefixes Optimal
Intuition
reachable[i] means "the first i characters can be split into dictionary words". A position is reachable when some earlier reachable position is followed by a dictionary word ending here — so every end position looks back over the starts already proven. Greedy longest-match fails ("cars" as car + s, when ca + rs was the split), which is why the lookback is not optional.
Algorithm
1. Put the dictionary in a set; the empty prefix is reachable. 2. For each end position, scan earlier reachable starts. 3. If the substring between them is a word, mark the end reachable and stop scanning. 4. The answer is whether the whole string is reachable.
Time & Space
Time O(n^2) substring checks against a set. Space O(n) plus the dictionary. Memoised DFS is the same idea upside down.