Last active
January 6, 2020 11:20
-
-
Save iykazrji/104f8c08d439cd3072b8afeaaa492702 to your computer and use it in GitHub Desktop.
Iyk Azorji Interview code response
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
/* | |
* Assume we have a list of words from the English dictionary | |
* [“water”, “big”, “apple”, “banana”, “york”, “amsterdam”, “orange”, “machintosh”, “bottle”, “book”] | |
* And another long list go string to process. | |
* Write a function that will identify “compound words” and return them | |
* Input: [“paris”, “applewatch”, “ipod”, “bigbook”, “orange”, “waterbottle”] | |
* Output: [“applewatch”, “bigbook”, “waterbottle”] | |
*/ | |
const dictionary = ["water", "big", "apple", "banana", "york", "amsterdam", "orange", "machintosh", "bottle", "book"]; | |
const input = ["paris", "applewatch", "ipod", "bigbook", "orange", "waterbottle"]; | |
const compoundWordIdentifier = (input)=>{ | |
let output = new Set(); | |
input.forEach((value)=>{ | |
dictionary.forEach((word)=>{ | |
if(word.length >= value || word === value){ | |
return; | |
} | |
if(word === value.substr(0, word.length)) { | |
const restStr = value.substr(word.length, value.length); | |
if(dictionary.includes(restStr)){ | |
output.add(value) | |
} | |
} | |
}); | |
}); | |
return Array.from(output); | |
} | |
compoundWordIdentifier(input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment