class Solution:
def wordBreak(self, s: 'str', wordDict: 'List[str]') -> 'bool':
dp = [False] * (len(s)+1)
dp[0] = True #初始化,空串能被任意拆分
for i in range(1, len(s)+1):
for j in range(0, i):
if dp[j] and s[j:i] in wordDict:
dp[i] = True
break #得到一个i拆分即可,进入i+1
return dp[len(s)]
class Solution:
def wordBreak(self, s: 'str', wordDict: 'List[str]') -> 'bool':
dp = [False] * (len(s)+1)
dp[0] = True
maxLen = 0
for word in wordDict:
maxLen = max(maxLen, len(word))
for i in range(1, len(s)+1):
for j in range(max(i-maxLen, 0), i):
if dp[j] and s[j:i] in wordDict:
dp[i] = True
break
return dp[len(s)]