Does anyone know how I can match an array of jumbled letters to a word, for example, some function that will match an array eg ["a","c","a","e","c"]; to a word "ace" and give me 1 or if not -1 like indexOf or InArray but for a jumbled word. I've made a js fiddle with an example it is well documented
just a note, I'll be comparing the array of letters to anywhere from 30000 - 50000 words.
https://jsfiddle.net/AlexanderMitrakis/89dchpt8/1/
this.gameletters = []; //Array of Game letters.
//e.g. ["P", "E", "H", "E", "U", "I", "S", "Q", "N"];
this.possiblesolution = new String(); //highest solution within gameletters
//e.g. "QUEENSHIP" (related to above letters)
this.wordBank = new Array();
//array of arrays structure is formated around alphabet with an array for each character:
/*
a: Array(7295)
b:Array(7271)
c:Array(11381)
d:Array(7216)
...
y:Array(607)
z:Array(623)
*/
A recursive strategy is a simple solution, but if your gameletters array gets too big, it will really slow down the execution. For a game like scrabble, it should be adequate though.
var gameletters = ["P", "E", "H", "E", "U", "I", "S", "Q", "N"];
var wordbank = {
"a": Array(3461),
"b": Array(2391),
//...
};
var matches = {};
function step(word, letters) {
for(var i = 0, len = letters.length; i < len; i++) {
var arr = letters.map(a => a);
if (arr.length === 0) {
return;
}
var letter = arr[i];
arr.splice(i,1);
test(word + letter);
if (arr.length) {
step(word + letter, arr)
}
}
}
function test(word) {
var firstLetter = word.substr(0,1);
if (wordbank[firstLetter].indexOf(word) >= 0) {
matches[word] = 1;
}
}
step("", gameletters);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.