How to efficiently generate words of length 2 characters?
2 answers
I'm not sure if I understood your question correctly, but this might help:
List<string> GetWords(IEnumberable<char> characters) {
char[] chars = characters.Distinct().ToArray();
List<string> words = new List<string>(chars.Length*chars.Length);
foreach (char i in chars)
foreach (char j in chars)
words.Add(i.ToString() + j.ToString());
return words;
}
+5
source to share
Are you talking about finding real two-character words that can be made from any combination of a list of characters?
In this case, you need to write an algorithm that can work out all possible combinations from the letters provided, and for each combination try (and vice versa) against IDictionary, which acts like a real dictionary of real two-word words.
Unverified code:
IDictionary<string, string> dictionary = GetRealTwoLetterWordDictionary();
char[] availableChars = new char[] { 'a', 's', 't' };
string[] combinations = GetAllCombinations(availableChars);
IList<string> results = new List<string>();
foreach (string combination in combinations)
{
if (dictionary.ContainsKey(combination)))
{
results.Add(combination);
}
string reversed = combination.Reverse();
if (dictionary.ContainsKey(reversed)))
{
results.Add(reversed);
}
}
0
source to share