Fetch the repository succeeded.
// 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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。