DFS
public boolean wordBreak(String s, List<String> wordDict) {
int[] tmp = new int[s.length()];
return dfs(s, 0, 1, wordDict, tmp);
}
public boolean dfs(String s, int i, int j, List<String> wordDict, int[] tmp ) {
if(i == s.length() || tmp[i] == 1) return true;
if(j == s.length() + 1 || tmp[i] == 2) return false;
if(wordDict.contains(s.substring(i, j))) {
boolean is = dfs(s, j, j + 1, wordDict, tmp) || dfs(s, i, j + 1, wordDict, tmp);
tmp[i] = is == true? 1 : 2;
return is;
}
return dfs(s, i, j + 1, wordDict, tmp);
}
BFS
DP