[LeetCode]140 单词拆分II

题目描述

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

  • 分隔时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例1

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]

示例2

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

示例3

输入:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
[]

思路

  • 动态规划查找是否为分割点
  • 深度优先遍历找出一组分割

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
Solution.res = []
self.dfs(s, wordDict, '')
return Solution.res

def dfs(self, s, wordDict, stringlist):
if self.check(s, wordDict):
if len(s) == 0:
Solution.res.append(stringlist[1:])
for i in range(1, len(s)+1):
if s[:i] in wordDict:
self.dfs(s[i:], wordDict, stringlist+' '+s[:i])

def check(self, s, wordDict):
dp = [False for i in range(len(s)+1)]
dp[0] = True
for i in range(len(s)):
for j in range(i, -1, -1):
if dp[j] and s[j:i + 1] in wordDict:
dp[i + 1] = True
break
return dp[len(s)]