139. Word Break

Source: Internet
Author: User

Given A string s and a Dictionary of words dict, determine if s can segmented into a space-s Eparated 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" .

Idea: Recursion. No Add if (S.equals (")) time has been an error, thought to finally understand why. Like code finally contains, go to another layer loop if no base case jumps directly into the For loop, return false. Note the base case.

 Public classSolution { Public BooleanWordbreak (String s, set<string>worddict) {        if(s==NULL|| worddict==NULL)        {            return false; }        if(S.equals (""))        {            return true; }         for(intI=0;i<s.length (); i++)        {             if(Worddict.contains (S.substring (0,i+1)))             {                 if(Wordbreak (S.substring (i+1) ( worddict)) {return true; }                 Else{worddict.remove (s.substring (0,i+1)); }             }        }        return false; }}

139. Word Break

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.