首页 > 代码库 > Leetcode-Word Break II

Leetcode-Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

Analysis:

Use DP. d[i] = sum of (d[j]*s[j...i]) for j=i-1...0

Leetcode-Word Break II