Created
April 7, 2020 08:55
-
-
Save ishan123456789/e0e15ad560df1a7600e8e0fe19c96b49 to your computer and use it in GitHub Desktop.
Combination of valid word from a dictionary
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 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