1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class WordBreakII { public List<String> wordBreak(String s, Set<String> wordDict) { List<String> result = new ArrayList<String>(); if (s == null || s.isEmpty()) { return result; } List<String> map[] = new ArrayList[s.length() + 1]; map[0] = new ArrayList<String>(); for (int i = 0; i < s.length(); i++) { if (map[i] == null) { continue; } for (String str : wordDict) { int l = str.length(); int end = i + l; if (end > s.length()) { continue; } if (s.substring(i, end).equals(str)) { if (map[end] == null) { map[end] = new ArrayList<String>(); } map[end].add(str); } } } if (map[s.length()] == null) { return result; } helper(s.length(), result, new ArrayList<String>(), map); return result; } private void helper(int end, List<String> result, List<String> crt, List<String> map[]) { if (end <= 0) { StringBuilder sb = new StringBuilder(); for (int i = crt.size() - 1; i >= 0; i--) { String s = crt.get(i); sb.append(s).append(' '); } sb.deleteCharAt(sb.length() - 1); result.add(sb.toString()); } for (String s : map[end]) { List<String> tmp = new ArrayList<String>(crt); tmp.add(s); helper(end - s.length(), result, tmp, map); } } }
|