Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
class Solution {
public boolean wordBreak(String s, List wordDict) {
boolean[] f = new boolean[s.length() + 1];
f[0] = true;
for(int i = 0; i <= s.length(); i++){
for(int j = i; j < s.length(); j++){
if(f[i] && wordDict.contains(s.substring(i, j + 1))){
f[j + 1] = true;
}
}
}
return f[s.length()];
}
}
标签:dictionary,WordBreak,length,boolean,words,wordDict,139,true From: https://www.cnblogs.com/MarkLeeBYR/p/16908779.html