题目链接:LeeCode139单词拆分
题目描述:
建立一个标记数组判断到当前位置字符串是否合法,在dp数组建立时只有当前字符串开头位置合法的时候才能计算当前合法串的长度并标记新得合法位置,返回结尾的合法情况就能得到答案
class Solution {
public static boolean wordBreak(String s, List<String> wordDict) {
boolean[] dpp=new boolean[s.length()+1];
dpp[0]=true;
for (int i = 0; i < s.length(); i++) {
if(dpp[i])
for (int j = i; j < s.length(); j++) {
if(wordDict.contains(s.substring(i,j+1))){
dpp[j+1]=true;
}
}
}
return dpp[s.length()];
}
}