package pat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
HashMap<String, ArrayList<String>> m = new HashMap<>();
return helper(s, wordDict, m);
}
public List<String> helper(String s, List<String> wordDict, HashMap<String, ArrayList<String>> m) {
ArrayList<String> res = new ArrayList<>();
if (m.containsKey(s))
return m.get(s);
if (s.length() == 0) {
res.add("");
return res;
}
for (String word : wordDict) {
if (s.startsWith(word)) {
for (String str : helper(s.substring(word.length()), wordDict, m)) {
res.add(word + (str == "" ? "" : " ") + str);
}
}
}
m.put(s, res);
return res;
}
}