140 Word Break II
140. Word Break II
1. Question
Given a non-empty strings and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictionary does not contain duplicate words.
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"]
.
2. Implementation
(1) DFS + Memoization
3. Time & Space Complexity
时间复杂度O(2^n), 考虑字典[a, aa, aaa, aaaa, aaaaa], input = "aaaaa....", 总共有2^n个解
空间复杂度O(mn), m是dict的word个数,n是input string的长度
Last updated