Created
January 28, 2023 00:29
-
-
Save germanescobar/696782acceb907a166dc3fccf85ebecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findAllConcatenatedWordsInADict = function(words) { | |
const result = [] | |
for (let i=0; i < words.length; i++) { | |
if (findWord(i, words[i], words)) { | |
result.push(words[i]) | |
} | |
} | |
return result | |
}; | |
function findWord(idx, word, words) { | |
if (word.length === 0) return true | |
for (let i=0; i < words.length; i++) { | |
if (idx !== i && word.startsWith(words[i])) { | |
const substr = word.substring(words[i].length) | |
if (findWord(idx, substr, words)) { | |
return true | |
} | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment