LeetCode #140: Word Break II
2016-09-03 13:07
330 查看
Problem Statement
(Source)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
The idea is to use adparray to compute and save: for index
iin the input
s, whether the substring
s[ : i + 1]can be broken into valid dictionary words. If it can, then also stores the start index of last break in another helper array
start.
After finishing computing above two arrays, the first thing is to see whether
dp[-1]is
True. If it is not, this means the original input
scannot be broken into valid dictionary words. Otherwise, it can.
If the input
scan be broken into valid dictionary words, then backtrack the helper array
startto construct the final result.
Solution
class Solution(object): def wordBreak(self, s, wordDict): """ :type s: str :type wordDict: Set[str] :rtype: List[str] """ from copy import deepcopy if not s or not wordDict: return [] min_len, max_len = float('inf'), float('-inf') for word in wordDict: min_len = min(min_len, len(word)) max_len = max(max_len, len(word)) # dp. n = len(s) dp = [False] * n start = [[] for i in xrange(n)] for end_index in xrange(min_len - 1, n): start_index = end_index - min_len + 1 while start_index >= 0 and end_index - start_index + 1 <= max_len: word = s[start_index : end_index + 1] if word in wordDict and (start_index == 0 or dp[start_index - 1]): dp[end_index] = True start[end_index].append(start_index) start_index -= 1 # bt: construct result. def helper(s, dp, start, end_index, res, ass): if end_index >= 0: for start_index in start[end_index]: word = s[start_index : end_index + 1] ass2 = deepcopy(ass) if not ass2: ass2.append([word]) else: for a in ass2: a.append(word) if start_index == 0: for a in ass2: a.reverse() res.append(' '.join(a)) else: helper(s, dp, start, start_index - 1, res, ass2) res = [] helper(s, dp, start, n - 1, res, []) return res
相关文章推荐
- 140. Word Break II(dp,字典匹配,并输出所有匹配结果,即保存dp路径)(继续理解,重刷)
- 【LeetCode】140. Word Break II
- Hard-题目45:140. Word Break II
- leetcode 140: Word Break II
- 140. Word Break II
- LeetCode 140. Word Break II
- leetcode 140. Word Break II
- leetcode@ [139/140] Word Break & Word Break II
- LeetCode 140. Word Break II(单词切分)
- leetcode 140. Word Break II
- 140.Word Break II
- 140. Word Break II
- 140. Word Break II
- 140. Word Break II
- LeetCode 140. Word Break II (DP+DFS)
- LeetCode(140) Word Break II
- Leetcode 140. Word Break II
- leetcode || 140、Word Break II
- [leetcode] 140. Word Break II 解题报告
- Leetcode140:Word Break II