How to find a word from arrays of characters?

前端 未结 4 1434
感动是毒
感动是毒 2020-12-14 04:55

What is the best way to solve this:

I have a group of arrays with 3-4 characters inside each like so:

{p,     {a,    {t,    {m,
 q,      b,     u,            


        
4条回答
  •  一整个雨季
    2020-12-14 05:10

    There are probably many way of solving this.

    What you are interested in is the number of each character you have available to form a word, and how many of each character is required for each dictionary word. The trick is how to efficiently look up this information in the dictionary.

    Perhaps you can use a prefix tree (a trie), some kind of smart hash table, or similar.

    Anyway, you will probably have to try out all your possibilities and check them against the dictionary. I.e., if you have three arrays of three values each, there will be 3^3+3^2+3^1=39 combinations to check out. If this process is too slow, then perhaps you could stick a Bloom filter in front of the dictionary, to quickly check if a word is definitely not in the dictionary.

    EDIT: Anyway, isn't this essentially the same as Scrabble? Perhaps try Googling for "scrabble algorithm" will give you some good clues.

提交回复
热议问题