Word Break
问题链接[https://leetcode.com/problems/word-break/]
给定:
1. 字符串 s = "leetcode",
2. 字典 dict = ["leet", "code"].
判断字符串是否能分解成一个或多个字典里的单词。
Return true because "leetcode" can be segmented as "leet code".
经典的动态规划问题,一直想写下来,一直都没时间写,现在先记下来,以后有空补上
class Solution:
# @param s, a string
# @param wordDict, a set<string>
# @return a boolean
def wordBreak(self, s, wordDict):
s_len=len(s)
DP=[False]*(s_len+1)
DP[0]=True
for i in range(s_len+1):
for j in range(i):
if(s[j:i] in wordDict and DP[j]):
DP[i]=True
return DP[s_len]
DP[i]==True 表示到第i个字符为止,该字符串能由字典里的单词组成