Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created January 28, 2023 00:29
Show Gist options
  • Save germanescobar/696782acceb907a166dc3fccf85ebecb to your computer and use it in GitHub Desktop.
Save germanescobar/696782acceb907a166dc3fccf85ebecb to your computer and use it in GitHub Desktop.
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