Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
concatenated-words.cpp 904 Bytes
Copy Edit Raw Blame History
kamyu authored 2016-12-20 16:59 +08:00 . Create concatenated-words.cpp
// Time: O(n * l^2)
// Space: O(n * l)
class Solution {
public:
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
unordered_set<string> lookup(words.begin(), words.end());
vector<string> result;
for (const auto& word : words) {
vector<bool> dp(word.length() + 1);
dp[0] = true;
for (int i = 0; i < word.length(); ++i) {
if (!dp[i]) {
continue;
}
for (int j = i + 1; j <= word.length(); ++j) {
if (j - i < word.length() && lookup.count(word.substr(i, j - i))) {
dp[j] = true;
}
}
if (dp[word.length()]) {
result.emplace_back(word);
break;
}
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search