https://oj.leetcode.com/problems/word-break-ii/
http://blog.csdn.net/linhuanmars/article/details/22452163
public class solution { public list< String> wordbreak (string s, set<string> dict) { // SOLUTION&NBSP;A:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;RETURN&NBSP;WORDBREAK_NP (s, dict) ; // SOLUTION&NBSP;B:&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;WORDBREAK_DP (s, dict); } /////////////////////////////// // Solution B: DP // &NBSP;&NBSP;PUBLIC&NBSP;LIST<STRING>&NBSP;WORDBREAK_DP (string s, set<string> dict) { s = "#" + s; int len = s.length (); solu[] solutions = new Solu[len]; // Init the first one; Solutions[0] = new solu (); solutions[0].exist = true; for (int i = 1 ; i < len ; i ++) { Solutions[i] = new&nbsP Solu (); for (int k = 0 ; k < i ; k ++) { if (solutions[k].exist) { string str = s.substring (k + 1, i &NBSP;+&NBSP;1); if (Dict.contains (str)) { &nbSp; solutions[i].exist = true ; solutions[i].froms.add (k); } } } } // Build results List<List<String>> sens = new ArrayList<List< String>> (); for (int i = 0 ; i < len ; i ++) { sens.add (new arraylist<string> ()); } sens.get (0). Add (""); buildresults (Sens, solutions, len - 1, s); return sens.get (len - 1); } private void buildresults (List<List< String>> sens, solu[] solutions, int n, string s) { for (Int i : solutions[n].froms) { if (Sens.get (i). IsEMpty ()) Buildresults (sens, solutions, i, s); string newstr = s.substring (i + 1, n + 1); for (String Oldstr : sens.get (i)) { sens.get (N). Add ( oldstr + " " + newstr). Trim ()); } } } static class Solu { booleaN exist; list<integer> froms = new ArrayList<> ( } ///////////////////////////////); // Solution A: NP // PUBLIC&NBSP;LIST<STRING>&NBSP;WORDBREAK_NP (string s, set<string> dict) { List<String> result = new ArrayList<> (); help (s), dict, "", 0, result); return result; } private void help (String s, Set<String> dict, String cur, int start, List<String> result) { if (Start >= s.length ()) { result.add (cur); return; } for (Int i = start ; i < s.length () ; i ++) { string temp = s.substring (start, i + 1); if (Dict.contains (temp)) { string nstr = cur + " " + temp; help (s, dict, Nstr, i + 1, result); } } }}
[Leetcode]140 Word Break II