func wordBreak(s string, wordDict []string) bool {
wordMap := make(map[string]bool)
n := len(s)
for _, word := range wordDict {
wordMap[word] = true
}
dp := make([]bool, n+1)
dp[0] = true
for i := 1; i <= n; i++ { // 遍历背包
for j := 0; j <= i; j++ { // 遍历物品
w := s[j:i] //截取字符串
if wordMap[w] && dp[j] {
dp[i] = true
break
}
}
}
return dp[n]
}