class Solution:
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
n = len(s)
dp = [False] * (n + 1)
# 初始条件
dp[0] = True
# 状态转移方程dp[i] = dp[j] and s[j:i] in wordDict for j in range(0, i)
for i in range(1, n + 1): # 对于每个i需遍历0-i之前是否有j使得dp[j]==True并将s[j:i]在wordDict里
for j in range(0, i):
if dp[j] and s[j:i] in wordDict:
dp[i] = True
break
return dp[n]