Little puzzlers–list all anagrams in a Word

Source: Internet
Author: User

The solution

A major hint is in the fact that we are given a dictionary. Because We is given this dictionary we can prep it on any-to-do-the-  Any word input becomes simple. Most dictionaries would easily fit within memory of modern computing hardware, so this should is not a major concern.

So, having the dictionary, we had all possible valid word anagrams already. The only trick is what get from the input to these words.

So let's think, "What does all anagrams has in common?"  For example, the word "post" has "pots", "stop", "tops", etc.  It may seem obvious and all anagrams has the same letters.  Thus, if we can hash or organize these letters in a consistent it, we can store all anagrams under the same key. The This is the we can apply the same hash/organization to the input word, immediately get the list of anagrams, and simply E Xclude the input Word from the anagram list.

The simplest-the-would simply be-to sort the letters, which is a fairly simple operation in C #:

var string. Concat (Word. ToLower (). (c = c));

That's would turn "post", "pots", etc. All to: "opst" which we can use as our keys for our Looku P. Now, what to store them, could download your own multidictionary, or create a dictionary<string, list<str  Ing>>, butreally C # already have one for you called a Lookup. The Lookup stores a key to multiple values.

So first, let's write a simple DAO for reading in we words from a file:

 Public classWordfiledao:iworddao { Public stringFileName {Get;Private Set; }  PublicWordfiledao (stringfileName) {FileName=FileName; }      Publicienumerable<string>retrievewords () {//Make sure-remove any accidental space and normalize-lower case        returnFile.readlines (FileName). Select (L =L.trim ().     ToLower ()); } } 

Then, we can create a anagram finder to create the lookup in construction, and then find words on Demand:

 Public classAnagramfinder {Private ReadOnlyilookup<string,string>_anagramlookup;  Publicanagramfinder (Iworddao dao) {//construct the lookup at initialization using the//dictionary words with the sorted letters as the key_anagramlookup =DAO. Retrievewords (). ToLookup (k=string. Concat (K.orderby (c =c)); }      Publicilist<string> Findanagrams (stringword) {         //at lookup time, sort the input word as the key,//and return all words (minus the input word) in the sequence        stringinput =Word.         ToLower (); stringKey =string. Concat (input. (c =c)); return_anagramlookup[key]. Where (w = w! =input).     ToList (); } }

Notice the tolookup () extension method (in system.linq), this method creates a instance of lookup from any ienumerable<t> if you provide it a key generator and a value generator.  For the key generator, I ' m returning the word in sorted, lowercase (for consistency).  For the value, I ' m just lower-casing the word (again, for consistency). This effectively creates the "Dictionary of a key to List of values" which, when you query using the "[...]" operator, Would return an IEnumerable<T> of the values, or empty sequence if the key is not found.

And that ' s it! We have our anagram word Finder which can lookup words quickly with only the cost of sorting the letters in a word, which is much less expensive (processing-wise) than attempting all permutations of the letters in a word.

Quote from:

Solution to Little puzzlers– "List all anagrams in a Word"

Little puzzlers–list all anagrams in a Word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.