Suppose you just need to find all possible combinations of words from a given dictionary that satisfy the condition. Without being distracted by the features of the language.
Important to search:
- the presence of letters in the word. We exclude the containing letters outside the given. We exclude, as far as compiling a phrase, already used.
- count the letters. At any moment in the compilation of a phrase, it is known which words are suitable for what length, or are definitely not suitable.
- duplicate letters.
Not important :
- the order of the letters in the dictionary word.
- meaning of the word.
For speed, you need to make the search for important features as fast as possible, if necessary, removing unimportant aspects.
To quickly find words with a suitable set of letters, but without counting repetitions, you can make a bit index, as suggested by @Mirdin: 26 letters of the English alphabet = 26 bits. Number the words in the dictionary (or simply count the line number as an index) and make a separate index in two columns: the word id is the bit mask of the existing letters. For a dictionary less than 65 thousand words, such an index will "weigh" 6 bytes per word, less than 400k. Can be kept in RAM for near-instant search. So you can quickly find, for example, the first word of a phrase — simply so that the bits of the “extra” letters are turned off.
It is worth making a copy of the dictionary, where the letters of the word are sorted alphabetically , and the words are sorted alphabetically. Those. again a separate index: сортированные_буквы - id_слова
. This index will be heavier than the dictionary itself (the number of words * 2 or 3 bytes). In this index, you can quickly find the right words and discard accurately-inappropriate.
Algorithm like this. We are looking for the first word. I want to find the first word of the greatest possible length. Search in length, from larger to smaller. Eats a valid set of letters and length. Found the first word, updated the permissible set of letters and the length of words - look for the next word.