題目鏈接
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
解法的兩大難點,定義dp數(shù)組跟找出狀態(tài)轉(zhuǎn)移方程纱耻,先來看dp數(shù)組的定義仙蛉,這里我們就用一個一維的dp數(shù)組,其中dp[i]表示范圍[0, i)內(nèi)的子串是否可以拆分观挎,注意這里dp數(shù)組的長度比s串的長度大1,是因為我們要處理空串的情況段化,我們初始化dp[0]為true嘁捷,然后開始遍歷。注意這里我們需要兩個for循環(huán)來遍歷穗泵,我們必須要遍歷所有的子串普气,我們用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)算出來了仔沿,可以直接用坐桩,只需要在字典中查找s.substr(j, i-j)是否存在了,如果二者均為true封锉,將dp[i]賦為true绵跷,并且break掉,此時就不需要再用j去分[0, i)范圍了成福,因為[0, i)范圍已經(jīng)可以拆分了碾局。最終我們返回dp數(shù)組的最后一個值,代碼如下:
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();
}
};