Overview
This is a partitioning problem. We want to know if the string `s[0...i]` can be broken up into valid dictionary words.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Given a string and a dictionary of words, determine if the string can be segmented into a space-separated sequence of dictionary words.
This is a partitioning problem. We want to know if the string `s[0...i]` can be broken up into valid dictionary words.
`dp[i]` is a boolean indicating whether the prefix of length `i` is breakable. We use an array of size `N + 1`.
To find if `dp[i]` is true, we test all possible split points `j` (from 0 to i-1). If `dp[j]` is true, AND the remaining substring `s[j...i]` is in the dictionary, then `dp[i]` is true!
As soon as we find ONE valid `j` that makes `dp[i]` true, we can immediately `break` the inner loop and move on to the next `i`. We just need to know IF it's possible, not how many ways.