22.8 C
New York
Monday, September 1, 2025

algorithm – How would I discover the perfect set of letters for my phrase recreation?


We are able to write a program to determine this out.

First, we’ll want an inventory of phrases we contemplate legitimate. You possibly can seize a Scrabble dictionary file and bolt on an inventory of widespread names. For a fast take a look at, I grabbed the primary conveniently formatted glossary my search turned up.

Then we are able to write some code that reads all these phrases, and converts them into bitsets – every phrase provides us a binary quantity the place the $i^{th}$ bit is one if the phrase incorporates the $i^{th}$ letter of the alphabet, and nil in any other case.

// Break up textual content file into an array of strings, one string per line.
var phrases = wordSource.textual content.Break up(new char[]{'r', 'n'},System.StringSplitOptions.RemoveEmptyEntries);

// Make an array of unsigned integers of the identical size, to be our bitsets.
_letterSets = new uint[words.Length];

// Iterate over the phrases.
for (int i = 0; i < phrases.Size; i++) {
    // Iterate over the letters of the phrase to construct its bitset.
    uint letters = 0;
    var phrase = phrases[i];
    foreach(char c in phrase) = 1u << (c - 'a');
    
    _letterSets[i] = letters;
}

Then we are able to iterate over all subsets of 8 letters from the 26 letters of the alphabet like so…

// Begin with the primary 8 letters of the alphabet:
// 0000 0000 0000 0000 0000 0000 1111 1111
//                               hgfe dcba
uint currentSet = (1 << 8) - 1;
// Advances _currentSet to the following subset of 8 letters and returns true,
// or returns false if we have checked each subset.
// Utilizing the trick from right here:
// https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation
bool MoveNext(ref uint currentSet)  (currentSet - 1);

    // There are quicker methods to do that, however exhibiting it this solution to be express.
    int trailingZeros = 0;
    whereas ((_currentSet & (1 << trailingZeros)) == 0)
        trailingZeros++;

    _currentSet = (uint)((t + 1) 

For every subset of 8 letters, we are able to rating what number of phrases will be spelled with that set:

int ScoreSet(uint currentSet) {
    int matchCount = 0;
    foreach(var set in _letterSets) {
        if ((set & _currentSet) == set)
            matchCount++;
    }
    return matchCount;
}

We’ll keep in mind the set with the very best depend we discovered, and report it because the output from our program.

This can be a fairly brute pressure technique – extra refined algorithms may do that extra effectively – however this was sufficient that I may let it run on an previous pill PC whereas making dinner and have my reply after I got here again to my desk.

Utilizing the glossary I linked, my code’s reply differs a bit from yours – it finds it is price sacrificing a vowel for a greater variety of consonants:

  • Vowels: AEI
  • Consonants: LNRST

These 8 letters can spell 4 678 of the 370 105 phrases within the checklist (about 1.26%), whereas AEIO/HRST can solely do 2 727 (about 0.74%).

Strive a technique like that with an inventory of phrases consultant of those you need to use in your recreation, and you will find the very best selections on your particular use case.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles