code_junkie

How to generate 2-characters long words effectively?

Nov 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. List<string> GetWords(IEnumberable<char> characters) {
  2. char[] chars = characters.Distinct().ToArray();
  3. List<string> words = new List<string>(chars.Length*chars.Length);
  4. foreach (char i in chars)
  5. foreach (char j in chars)
  6. words.Add(i.ToString() + j.ToString());
  7. return words;
  8. }
  9.  
  10. IDictionary<string, string> dictionary = GetRealTwoLetterWordDictionary();
  11. char[] availableChars = new char[] { 'a', 's', 't' };
  12. string[] combinations = GetAllCombinations(availableChars);
  13. IList<string> results = new List<string>();
  14.  
  15. foreach (string combination in combinations)
  16. {
  17. if (dictionary.ContainsKey(combination)))
  18. {
  19. results.Add(combination);
  20. }
  21.  
  22. string reversed = combination.Reverse();
  23.  
  24. if (dictionary.ContainsKey(reversed)))
  25. {
  26. results.Add(reversed);
  27. }
  28. }
Add Comment
Please, Sign In to add comment