单词拆分

bool wordBreak(string s, vector<string>& wordDict) {
std::unordered_set<std::string> wordSet;
for(auto& it : wordDict) {
wordSet.insert(it);
}
int sz = s.size();
std::vector<bool> dp(sz + 1, false);
dp[0] = true;
for (int i = 1; i <= sz; ++i) {
for (int j = 0; j < i; ++j) {
if(dp[j] && wordSet.find(s.substr(j, i - j)) != wordSet.end()) {
dp[i] = true;
break;
}
}
}
return dp[sz];
}

浙公网安备 33010602011771号