class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
unordered_set<string> hash(wordDict.begin(), wordDict.end());
vector<bool> dp(s.size() + 1, false);
dp[0] = true;
for(int i = 0; i < s.size(); i++) {
if(!dp[i]) continue;
for(int j = i + 1; j <= s.size(); j++) {
if(hash.find(s.substr(i, j - i)) != hash.end())
dp[j] = dp[i];
}
}
return dp.back();
}
};
代码随想录46天|139. 单词拆分
最新推荐文章于 2024-07-25 22:25:49 发布