-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcatenatedWords.cpp
44 lines (42 loc) · 1.24 KB
/
ConcatenatedWords.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<string> res;
unordered_set<string> dict(words.begin(), words.end());
for(auto word : words){
int n = word.size();
if(n == 0){
continue;
}
bool dp[n+1];
memset(dp, false, sizeof(dp));
dp[0] = true;
for(int i = 0; i<n; i++){
if(dp[i] == false){
continue;
}
string generate = "";
for(int j = i; j<n; j++){
generate += word[j];
if(dp[j+1] == true){
continue;
}
if(i == 0 && j == n-1){
continue;
}
if(dict.find(generate) != dict.end()){
dp[j+1] = true;
}
}
if(dp[n]){
res.push_back(word);
break;
}
}
}
return res;
}
};