See https://youtu.be/_-AfhLQfb6w and standupmaths/fiveletterworda
Every word having unique Latin letters could be represented as a mask, each letter being represented by its own bit.
26 bits is enough, I use int
that tends to have 32 bits. E.g. cat
is the set of letter 1, letter 3, and letter 20 that is 2^0 + 2^2 + 2^19 = 524293
.
So if two words having masks M1
and M2
share letters, then M1 & M2 != 0
. If they don't, M1 & M2 == 0
.
First, we build a mapping from masks to corresponding word lists (e.g. swing
and wings
have the same mask).
Then for each mask we build a list of masks not sharing bits with the mask (a word list not sharing any letter with the chosen word).
Then we repeat this step resursively building lists of masks not sharing bits with the chosen masks until the list become empty or we reach the 5th level. Reaching the 5th level we print the result.
Unrolling the recursion we get this:
Build map MASKS -> WORDS
For each M1 from MASKS
Build list MS2 := each M2 from MASKS if M1&M2=0
For each M2 from MS2
Build list MS3 := each M3 from MS2 if M1&M2&M3=0
For each M3 from MS3
Build list MS4 := each M4 from MS3 if M1&M2&M3&M4=0
For each M4 from MS4
Build list MS5 := each M5 from MS4 if M1&M2&M3&M4&M5=0
For each M5 from MS5
Print WORDS[M1], WORDS[M2], WORDS[M3], WORDS[M4], WORDS[M5]
-
Download the
words_alpha.txt
file from https://github.com/dwyl/english-words -
Compile the program:
g++ -o fiveletterwords --std=c++11 -O2 fiveletterwords.cpp
-
Run it:
fiveletterwords # like this fiveletterwords path/to/words_alpha.txt > fiveletterwords.txt # or like this
-
Get the result in seconds!
Note: http://www.gwicks.net/dictionaries.htm is cool too.