DFS on String
680. Split String (Lintcode)
public List<List<String>> splitString(String s) {
List<List<String>> res = new ArrayList<>();
if (s == null || s.isEmpty()) {
return res;
}
dfs(s, 0, res, new ArrayList<>());
return res;
}
private void dfs(String s, int index, List<List<String>> res, List<String> curt) {
if (index == s.length()) {
res.add(new ArrayList<>(curt));
return;
}
for (int i = index + 1; i <= index + 2 && i <= s.length(); i++) {
curt.add(s.substring(index, i));
dfs(s, i, res, curt);
curt.remove(curt.size() - 1);
}
}Last updated