-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path0472-concatenated-words.js
More file actions
35 lines (32 loc) · 971 Bytes
/
0472-concatenated-words.js
File metadata and controls
35 lines (32 loc) · 971 Bytes
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
/**
* 472. Concatenated Words
* https://leetcode.com/problems/concatenated-words/
* Difficulty: Hard
*
* Given an array of strings words (without duplicates), return all the concatenated words
* in the given list of words.
*
* A concatenated word is defined as a string that is comprised entirely of at least two
* shorter words (not necessarily distinct) in the given array.
*/
/**
* @param {string[]} words
* @return {string[]}
*/
var findAllConcatenatedWordsInADict = function(words) {
const set = new Set(words);
const map = new Map();
function isValid(word) {
if (map.has(word)) return map.get(word);
for (let i = 1; i < word.length; i++) {
const suffix = word.slice(i);
if (set.has(word.slice(0, i)) && (set.has(suffix) || isValid(suffix))) {
map.set(word, true);
return true;
}
}
map.set(word, false);
return false;
}
return words.filter(word => word.length > 0 && isValid(word));
};