139. Word Break
1.問題
2.想法
3.程式碼
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if (wordDict.empty()) {
return false;
}
unordered_set<string> mset(wordDict.begin(), wordDict.end());
int size = s.size();
vector<bool> dp(size + 1, false);
dp[0] = true;
for (int end = 0; end < size; end++) {
for (int start = end; start >=0; start--) {
if (dp[start] && mset.count(s.substr(start, end - start + 1))) {
dp[end + 1] = true;
break;
}
}
}
return dp[size];
}
};Last updated