We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 150c099 commit a0871e7Copy full SHA for a0871e7
472-concatenated-words.js
@@ -0,0 +1,27 @@
1
+/**
2
+ * @param {string[]} words
3
+ * @return {string[]}
4
+ */
5
+const findAllConcatenatedWordsInADict = function(words) {
6
+ let res = []
7
+ if (words === null || words.length == 0) return res
8
+ let set = new Set(words)
9
+ for (let word of words) {
10
+ set.delete(word)
11
+ if (dfs(word, set, '')) res.push(word)
12
+ set.add(word)
13
+ }
14
+ return res
15
+}
16
+
17
+function dfs(word, set, prev) {
18
+ if (prev != '') set.add(prev)
19
+ if (set.has(word)) return true
20
+ for (let i = 1; i <= word.length; i++) {
21
+ const prefix = word.substring(0, i)
22
+ if (set.has(prefix) && dfs(word.substring(i), set, prev + prefix)) {
23
+ return true
24
25
26
+ return false
27
0 commit comments