Skip to content

Commit

Permalink
Time: 215 ms (81.04%), Space: 49.3 MB (56.69%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Jan 28, 2023
1 parent b64fd41 commit 7efd09d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 0472-concatenated-words/0472-concatenated-words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
unordered_set<string> words_set;
for (string word : words) words_set.insert(word);
vector<string> res;

for (string word : words) {
int n = word.size();
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 0; i < n; i++) {
if (!dp[i]) continue;
for (int j = i + 1; j <= n; j++) {
if (j - i < n && words_set.count(word.substr(i, j - i))) {
dp[j] = 1;
}
}
if (dp[n]) {
res.push_back(word);
break;
}
}
}
return res;
}
};

0 comments on commit 7efd09d

Please sign in to comment.