題目鏈接
tag:
- Medium;
- DP;
question:
??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".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
思路:
??DP解法的兩大難點(diǎn),定義dp數(shù)組跟找出狀態(tài)轉(zhuǎn)移方程,先來(lái)看dp數(shù)組的定義,這里我們就用一個(gè)一維的dp數(shù)組,其中dp[i]表示范圍[0, i)內(nèi)的子串是否可以拆分,注意這里dp數(shù)組的長(zhǎng)度比s串的長(zhǎng)度大1,是因?yàn)槲覀円幚砜沾那闆r,我們初始化dp[0]為true,然后開(kāi)始遍歷。注意這里我們需要兩個(gè)for循環(huán)來(lái)遍歷,我們必須要遍歷所有的子串,我們用j把[0, i)范圍內(nèi)的子串分為了兩部分,[0, j) 和 [j, i),其中范圍 [0, j) 就是dp[j],范圍 [j, i) 就是s.substr(j, i-j),其中dp[j]是之前的狀態(tài),已經(jīng)算出來(lái)了,可以直接用,只需要在字典中查找s.substr(j, i-j)是否存在了,如果二者均為true,將dp[i]賦為true,并且break掉,此時(shí)就不需要再用j去分[0, i)范圍了,因?yàn)閇0, i)范圍已經(jīng)可以拆分了。最終我們返回dp數(shù)組的最后一個(gè)值,代碼如下:
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
// DP
unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
vector<bool> dp(s.size() + 1);
dp[0] = true;
for (int i = 0; i < dp.size(); ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && wordSet.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp.back();
}
};