Leetcode 193. Word Break I and II

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

思路:由于不要求把所有的word break列举出来,只求True or False。所以用DP即可。 这里DP[i]表明从开头的字幕到第i个字母所形成的string是breakable的。取决于

1. 如果DP[0:i+1] in wordDict, then DP[i] = True

2. 如果DP[j:i+1] in wordDict 并且 DP[j-1] == True, then DP[i] = True  (j!=0)

3. DP[i] = False

 1 class Solution(object):
 2     def wordBreak(self, s, wordDict):
 3         """
 4         :type s: str
 5         :type wordDict: List[str]
 6         :rtype: bool
 7         """
 8         DP = [False]*(len(s))
 9         
10         for i in range(len(s)):
11             for j in range(i+1):
12                 if j == 0 and s[:i+1] in wordDict:
13                     DP[i] = True
14                 elif DP[j-1] and s[j:i+1] in wordDict:
15                     DP[i] = True
16         
17         return DP[-1]

或者将DP list的长度初始化为n+1, DP[0]赋值为True。可以合并上面L12和L14两种情况。

 1 class Solution(object):
 2     def wordBreak(self, s, wordDict):
 3         """
 4         :type s: str
 5         :type wordDict: List[str]
 6         :rtype: bool
 7         """
 8         DP = [False]*(len(s)+1)
 9         DP[0] = True
10         
11         for i in range(len(s)):
12             for j in range(i+1):
13                 if DP[j] == True and s[j:i+1] in wordDict:
14                     DP[i+1] = True
15         
16         return DP[-1]

 第二题的思路: 由于需要把所有的解都列出来,所以很显然是一个DFS的题目。如果直接DFS的话会超时,先用word break I的DP方法判断一下是不是breakable。

 1 class Solution(object):
 2     def wordBreak(self, s, wordDict):
 3         """
 4         :type s: str
 5         :type wordDict: List[str]
 6         :rtype: List[str]
 7         """
 8         ans = []
 9         if self.isBreakable(s, wordDict) == True:
10             self.dfs(s, wordDict, '', ans)
11         return ans
12         
13     def dfs(self, s, wordDict, line, ans):
14         if len(s) == 0:
15             ans.append(line[:-1])
16         
17         for elem in wordDict:
18             size = len(elem)
19             if len(s) >= size and s[:size] == elem:
20                 self.dfs(s[size:], wordDict, line+elem+' ', ans)
21             
22     def isBreakable(self, s, wordDict):
23         """
24         :type s: str
25         :type wordDict: List[str]
26         :rtype: bool
27         """
28         DP = [False]*(len(s))
29         
30         for i in range(len(s)):
31             for j in range(i+1):
32                 if j == 0 and s[:i+1] in wordDict:
33                     DP[i] = True
34                 elif DP[j-1] and s[j:i+1] in wordDict:
35                     DP[i] = True
36         
37         return DP[-1]

 

posted @ 2017-03-08 14:18  lettuan  阅读(215)  评论(0编辑  收藏  举报