[Leetcode-java] Word Break II

Source: Internet
Author: User

Topic:

Given A string s and a Dictionary of words dict, add spaces in s to construct a sentence where Each of the word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog" ,
dict = ["cat", "cats", "and", "sand", "dog"] .

A solution is ["cats and dog", "cat sand dog"] .

Idea: According to the idea of word break, it is necessary to record the string at this time, and continue traversing instead of break, every time the DP array is set to True

Consider using HashMap to record, key for the corresponding DP position i,value for real-time feasible list<string>.

First, look at the word break before each position I can be break traversal substring all (0, J) (J, I).

The following two points need to be noted in the II:

(1) Need to determine whether the size of the dp[j] is 0 of course, only when the first slice of 00 is required

(2) For each time (0, J) splicing all need to cycle the entire map.get (j)

(3) because it is not the same as I in the judgement after the direct break, so in the traversal of the need to determine whether map.get (i) is empty, empty words are added directly, not empty then take out and traverse all the list of strings to add.

Code:

Public Class solution{ PublicList<string> Wordbreak (String s, set<string>worddict) {        //but still tle .        Boolean[] Isbreak =New Boolean[S.length () + 1]; isbreak[0] =true; Map<integer, list<string>> map =NewHashmap<integer, list<string>>();  for(inti = 0; I <= s.length (); i++) Map.put (i,NewArraylist<string>());  for(inti = 1; I <= s.length (); i++){             for(intj = 0; J <= I; J + +) {String cur=S.substring (J, I); if(Isbreak[j] &&worddict.contains (cur)) {Isbreak[i]=true; List<String> temp =NewArraylist<string>(); if(Map.get (j). Size () = = 0) {//Precautions (1)temp.add (cur); }                    Else{//Precautions (2)                         for(intk = 0; K < Map.get (j). Size (); k++) {Temp.add (Map.get (j). Get (k)+ " " +cur); }                    }                    //Precautions (3)                    if(Map.get (i). Size () = = 0) {map.put (i, temp); }Else{                         for(intk = 0; K < Map.get (j). Size (); k++) {map.get (i). Add (Temp.get (k)); }                    }                }            }        }        returnMap.get (S.length ()); }  }

Finally is in the Leetcode, there is a complex use case, directly with the above also will tle, need to first do word break in the judgment, whether can break, if you can use the above to get, AC code as follows:

 Public classSolution { PublicList<string> Wordbreak (String s, set<string>worddict) {        Boolean[] Isbreak =New Boolean[S.length () + 1]; isbreak[0] =true; Map<integer, list<string>> map =NewHashmap<integer, list<string>>();  for(inti = 0; I <= s.length (); i++) Map.put (i,NewArraylist<string>()); if(!isbreakhelper (S, worddict)) {//make a judgment first .            return NewArraylist<string>(); }Else{                 for(inti = 1; I <= s.length (); i++){             for(intj = 0; J <= I; J + +) {String cur=S.substring (J, I); if(Isbreak[j] &&worddict.contains (cur)) {Isbreak[i]=true; List<String> temp =NewArraylist<string>(); if(Map.get (j). Size () = = 0) {temp.add (cur); }                    Else{                         for(intk = 0; K < Map.get (j). Size (); k++) {Temp.add (Map.get (j). Get (k)+ " " +cur); }                    }                                        if(Map.get (i). Size () = = 0) {map.put (i, temp); }Else{                         for(intk = 0; K < Map.get (j). Size (); k++) {map.get (i). Add (Temp.get (k)); }                    }                }            }        }        }                returnMap.get (S.length ()); }     Public BooleanIsbreakhelper (String s, set<string>worddict) {        if(s = =NULL|| S.length () = = 0)            return true; Boolean[] req =New Boolean[S.length () + 1];//to the subscript I canReq[0] =true;//need to judge all the rest for 0 o'clock                 for(inti = 1; I <= s.length (); i++){                         for(intj = 0; J <= I; J + +){                if(Req[j] &&Worddict.contains (S.substring (J, i))) {Req[i]=true;  Break; }                             }        }                 returnreq[s.length ()]; }}

Changed the whole morning to defeat on this top t.t

[Leetcode-java] Word Break II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.