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.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = “leetcode”, wordDict = [“leet”, “code”]
Output: true
Explanation: Return true because “leetcode” can be segmented as “leet code”.
给出一个string s,一个字典,问s字符串是否能拆分成字典中的单词。
字典中的单词可以重复使用。
思路:
首先想top down思路
假设s=“leetcode”
有以下的拆分方法(题中说用space拆分,为了清晰这里用逗号):
l,eetcode
le, etcode
lee,tcode
leet,code
leetc,code 等等一直到最后leetcod,e
对于每一个左右拆分的子字符串,又递归上述的左右拆分方法
可以看到leetc,code中对左边leetc左右拆分时,又会遇到刚才的l,le,lee,leet
所以需要用hash保存每个特定的子字符串的可拆分flag
这个代码不做实现
转换回dp思路
用dp保存每一个index i对应的可拆分flag,左边补上对应“”的index 0
dp思路参考https://www.cnblogs.com/grandyang/p/4257740.html
中的解法2:
“dp[i]表示范围[0, i)内的子串是否可以拆分,注意这里dp数组的长度比s串的长度大1,是因为我们要handle空串的情况,我们初始化dp[0]为true,然后开始遍历。注意这里我们需要两个for循环来遍历,因为此时已经没有递归函数了,所以我们必须要遍历所有的子串,我们用j把[0, i)范围内的子串分为了两部分,[0, j) 和 [j, i),其中范围 [0, j) 就是dp[j],范围 [j, i) 就是s.substr(j, i-j),其中dp[j]是之前的状态,我们已经算出来了,可以直接取,只需要在字典中查找s.substr(j, i-j)是否存在了,如果二者均为true,将dp[i]赋为true,并且break掉,此时就不需要再用j去分[0, i)范围了,因为[0, i)范围已经可以拆分了。最终我们返回dp数组的最后一个值,就是整个数组是否可以拆分的布尔值”
c++代码
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> word_set(wordDict.begin(), wordDict.end());
vector<bool> dp(s.size() + 1, false);
dp[0] = true;
for(int i = 0; i < dp.size(); i++) {
for(int j = 0; j < i; j++) {
if (dp[j] && word_set.count(s.substr(j, i - j))) {
dp[i] = true;
}
}
}
return dp[s.size()];
}
java代码:
public boolean wordBreak(String s, List<String> wordDict) {
HashSet<String> wordSet = new HashSet<>(wordDict);
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 0; i < dp.length; i++) {
for(int j = 0; j < i; j++) {
if (dp[j] && wordSet.contains(s.substring(j, i))) {
dp[i] = true;
}
}
}
return dp[s.length()];
}