Solution
- Check if the word can be broke up to an index
i
. - For any substring end at
i
, we check if there exists any breakable word end atj
can be combined with a word inwordDict
to form it.j < i
because of DP.
j > i - max_len
because the prefix word needs to be long enough.
```python def wordBreak(self, s, wordDict): “”” :type s: str :type wordDict: List[str] :rtype: bool “”” breakable = [True] max_len = max(map(len,wordDict+[’’]))for i in range(1, len(s)+1): breakable += any(breakable[j] and s[j:i] in wordDict for j in range(max(0, i-max_len),i)), return breakable[-1] ```
PREVIOUSJump Game
NEXTMajority Element