Skip to content

Instantly share code, notes, and snippets.

@ishan123456789
Created April 7, 2020 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishan123456789/e0e15ad560df1a7600e8e0fe19c96b49 to your computer and use it in GitHub Desktop.
Save ishan123456789/e0e15ad560df1a7600e8e0fe19c96b49 to your computer and use it in GitHub Desktop.
Combination of valid word from a dictionary
var dic = require('./dic.json');
const stringToUse = "restaurant";
const arr = combinations(stringToUse);
const result = arr.filter(str => str.length> 1 && !!dic[str]).join(' ')
console.log(result);
// dictionary available at https://raw.githubusercontent.com/matthewreagan/WebstersEnglishDictionary/master/dictionary_compact.json
// https://codereview.stackexchange.com/a/7025/121772
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment