Implement an efficient program that reads in a list of words from the file wordl
ID: 3660782 • Letter: I
Question
Implement an efficient program that reads in a list of words from the file wordlist.txt and prints out all anagrams for a given string (taken as input from the user or otherwise assigned to a variable). As an example, the strings "comedian" and "demoniac" are anagrams of each other, i.e., re-orderings of the same set of characters.Explanation / Answer
public IList Matches(string sourceLetters, string [] wordList) { sourceLetters = sourceLetters.ToUpper(); IList matches = new List(); foreach (string word in wordList) { if (WordCanBeBuiltFromSourceLetters(word, sourceLetters)) matches.Add(word); } return matches; } public bool WordCanBeBuiltFromSourceLetters(string targetWord, string sourceLetters) { string builtWord = ""; foreach (char letter in targetWord) { int pos = sourceLetters.IndexOf(letter); if (pos >= 0) { builtWord += letter; sourceLetters = sourceLetters.Remove(pos, 1); continue; } // check for wildcard pos = sourceLetters.IndexOf("*"); if (pos >= 0) { builtWord += letter; sourceLetters = sourceLetters.Remove(pos, 1); } } return string.Equals(builtWord, targetWord); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.