递推式子不太好想 每个substring 都可以check leetcode l + eetcode , le + etcode, lee + tcode, ......
所以记录一个boolean数组 和string长度一样 每一位代表 从开头到这一位为止 是否存在字典里(并不需要一整个substring是属于一个词)
public class Solution {
public boolean wordBreak(String s, Set<String> wordDict) {
boolean [] temp = new boolean [ s.length() + 1 ];
Arrays.fill( temp, false );
temp[0] = true;
for ( int i = 1; i < temp.length; i ++ ){
for ( int j = 0; j < i; j ++ ){
if ( temp[j] && wordDict.contains(s.substring (j,i)) ){
temp [i] = true;
break;
}
}
}
return temp[s.length()];
}
}