参考题解
时间复杂度: O ( n 3 ) O(n^3) O(n3)
class Solution {
public boolean wordBreak(String s, List wordDict)
{
Set set = new HashSet<>(wordDict);
boolean[] f = new boolean[s.length() + 1];
Arrays.fill(f, false);
f[0] = true;
for (int i = 1; i <= s.length(); i++)//枚举背包容量
for (int j = 0; j < i && !f[i]; j++)//枚举物品
if (set.contains(s.substring(j, i)) && f[j])
f[i] = true;
return f[s.length()];
}
}