"Leetcode-Interview algorithm classic-java implementation" "139-word Break (word split)"

Source: Internet
Author: User

"139-word break (Word split)""leetcode-Interview algorithm classic-java Implementation" "All Topics folder Index"Original Question

Given a string s and a dictionary of words Dict, determine if s can segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode" ,
Dict = ["leet", "code"] .
Return true because "leetcode" can be segmented as "leet code" .

Main Topic

Given a string s and Word dictionary dict, determine if s can be cut into one or more words separated by a space-delimited sequence.

Thinking of solving problems

A string s, whose length is N. Assuming that s can be stitched together by a word in the "dictionary set" (dict), the conditions to be met are:
* F (0, N) = f (0, I) && f (i, J) && F (j, N);
* This way, suppose we want to know if a substring can be spliced by several words in the dict, and the achievement can be achieved in this way (satisfying the condition to true, not satisfying the condition to false) to the corresponding position in a Boolean array, the last Boolean The last digit of the array is the value of f (0, N), which is true to indicate that the string s can be spliced by the words in the dict, otherwise it will not work!

Code Implementation

Algorithm implementation class

ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Map;ImportJava.util.Set; Public  class solution {     Public Boolean Wordbreak(String s, set<string> worddict) {//Parameter check        if(s = =NULL|| S.length () <1|| Worddict = =NULL|| Worddict.size () <1) {return false; }//Mark matches, match[i] table [0, i-1] all match        intLength = S.length ();Boolean[] match =New Boolean[Length +1]; match[0] =true; for(inti =1; I < length +1; i++) { for(intj =0; J < I; J + +) {if(Match[j] && worddict.contains (s.substring (J, i)) {//F (0,n) = f (0,i) + f (i,j) + f (j,n)Match[i] =true; Break; }            }        }returnMatch[length]; }//The following is another solution. But it will time out .     Public Boolean WORDBREAK2(String s, set<string> worddict) {//Parameter check        if(s = =NULL|| S.length () <1|| Worddict = =NULL|| Worddict.size () <1) {return false; } map<character, set<string>> wordmap =NewHashmap<> (Worddict.size ());//Put all start characters in a set         for(String word:worddict) {set<string> Set = Wordmap.get (Word.charat (0));if(Set = =NULL) {//Create a new set into the mapSet =NewHashset<> (); Wordmap.put (Word.charat (0), set); }//Word in SetSet.add (word); }returnWordbreak (s),0, Wordmap); }/** * Search string can be cut into Word string * * @param s String * @param The start position of the IDX processing * @par Am Wordmap Word dictionary, starting character same as in the same set set * @return Search Results * /     Public Boolean Wordbreak(String S,intIDX, Map<character, set<string>> wordmap) {if(IDX >= s.length ()) {return true; } set<string> words = Wordmap.get (S.charat (IDX));if(Words! =NULL) { for(String word:words) {///IDX before the characters have been matched, assuming that the word word is matched from the IDE                if(S.startswith (Word, idx)) {//recursive processing                    Booleanresult = Wordbreak (s, idx + word.length (), wordmap);//Assuming the condition is met, returns True                    if(Result) {return true; }                }            }        }return false; }}
Assessment Results

  Click on the picture, the mouse does not release, drag a position, release after the new form to view the full picture.

Special InstructionsWelcome reprint. Reprint Please specify the source "http://blog.csdn.net/derrantcm/article/details/47774547"

"Leetcode-Interview algorithm classic-java implementation" "139-word Break (word split)"

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.