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