Word Break
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
if (s == null || s.length() == 0) {
return true;
}
boolean[] res = new boolean[s.length() + 1];
Arrays.fill(res, false);
res[0] = true;
int max = getMaxLength(dict);
for (int i = 1; i <= s.length(); i++) {
for (int j = 1; j <= i && j <= max; j++) {
if (!res[i - j]) {
continue;
}
String curString = s.substring(i - j, i);
if (dict.contains(curString)) {
res[i] = true;
break;
}
}
}
return res[s.length()];
}
private int getMaxLength(Set<String> dict) {
int max = 0;
for (String s : dict) {
if (max < s.length()) {
max = s.length();
}
}
return max;
}
}