Skip to content

Instantly share code, notes, and snippets.

@iykazrji
Last active January 6, 2020 11:20
Show Gist options
  • Save iykazrji/104f8c08d439cd3072b8afeaaa492702 to your computer and use it in GitHub Desktop.
Save iykazrji/104f8c08d439cd3072b8afeaaa492702 to your computer and use it in GitHub Desktop.
Iyk Azorji Interview code response
/*
* 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