-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode--472-ConcatenatedWords.js
54 lines (47 loc) · 1.43 KB
/
leetcode--472-ConcatenatedWords.js
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
45
46
47
48
49
50
51
52
53
54
// https://leetcode.com/problems/concatenated-words/
// p: array
// r: array
// e:
// Example 1:
// Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
// Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
// "dogcatsdog" can be concatenated by "dog", "cats" and "dog";
// "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
// Example 2:
// Input: words = ["cat","dog","catdog"]
// Output: ["catdog"]
var findAllConcatenatedWordsInADict = function (words) {
// let map = {};
const result = [];
for (let i = 0; i < words.length; ++i) {
let wordsClone = [...words];
wordsClone.splice(i, 1);
const arrTemp = [];
let wordTemp = words[i];
for (let j = 0; j < wordsClone.length; ++j) {
wordTemp.includes(wordsClone[j]) && arrTemp.push(wordsClone[j]);
}
if (arrTemp.length > 1) {
result.push(words[i]);
}
// (map[words[i]] = arrTemp);
console.log(arrTemp);
}
console.log(result);
return result;
};
// findAllConcatenatedWordsInADict(["cat", "dog", "catdog"]);
findAllConcatenatedWordsInADict([
"cat",
"cats",
"catsdogcats",
"dog",
"dogs",
"dogcatsdog",
"hippopotamuses",
"rat",
"ratcatdogcat",
"catsdogcatsmouse",
]);
// Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
// findAllConcatenatedWordsInADict(["a", "b", "ab", "abc"]);