Match function of broken array to string

Does anyone know how I can match an array of mixed letters with a word, for example, some function that will correspond to an array, for example ["a", "c", "a", "e", "c"]; to the word "ace" and give me 1 or, if not -1, like indexOf or InArray, but for a mixed word. I made a js fiddle with an example that is well documented

just a note, I will compare an array of letters anywhere from 30,000 to 50,000 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)
               */
+6
source share
1 answer

- , , . , , , .

Fiddle

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);
+1

All Articles