"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 Directory 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 the word dictionary dict, determine if s can be split into one or more word spaces separated by a sequence.

Thinking of solving problems

A string s, whose length is n, if 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, if we want to know if a substring can be spliced by several words in the dict, the achievement can be achieved in such a manner that the condition is true, the condition is false, and it is stored in the corresponding position of 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 words with the same starting character 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); }/** * Whether the search string can be split into a word string * * @param s String * @param The starting position of the IDX processing * @pa Ram Wordmap Word dictionary, starting with the same character 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, if the word word is matched from behind the IDE                if(S.startswith (Word, idx)) {//recursive processing                    Booleanresult = Wordbreak (s, idx + word.length (), wordmap);//Returns True if condition is met                    if(Result) {return true; }                }            }        }return false; }}
Evaluation Results

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

Special InstructionsWelcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47774547"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"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.