class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> set = new HashSet<>(wordDict);
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 0; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
String sub = s.substring(j, i);
if (set.contains(sub) && dp[j]) {
dp[i] = true;
}
}
}
return dp[s.length()];
}
}
代码随想录训练营第三十期|第四十六天|动态规划 part08|● 139.单词拆分 ● 关于多重背包,你该了解这些! ● 背包问题总结篇!
最新推荐文章于 2024-07-10 16:47:58 发布
![](https://img-home.csdnimg.cn/images/20240709112858.png)