Little Puzzlers–List All Anagrams in a Word

The Solution

A major hint is in the fact we are given a dictionary.  Because we are given this dictionary we can prep it in any way we like when the program starts, and then the processing of any word input becomes simple.  Most dictionaries will easily fit within memory of modern computing hardware, so this should not be a major concern.

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

So let’s think, “what do all anagrams have in common?”  For example, the word “post” has “pots”, “stop”, “tops”, etc.  It may seem obvious, but all anagrams have the same letters.  Thus, if we can hash or organize these letters in a consistent way, we can store all anagrams under the same key.  In this way, we can apply the same hash/organization to the input word, immediately get the list of anagrams, and simply exclude the input word from the anagram list.

The simplest way to do this would simply be to sort the letters, this is a fairly simple operation in C#:

var key = string.Concat(word.ToLower().OrderBy(c => c));

That would turn “post”, “pots”, etc. all into: “opst” which we can use as our key for our lookup.  Now, how to store them, you could download your own multidictionary, or create a Dictionary<string, List<string>>,but really C# already has 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 our words from a file:

public class WordFileDao : IWordDao
{
    public string FileName { get; private set; }

    public WordFileDao(string fileName)
    {
        FileName = fileName;
    }

    public IEnumerable<string> RetrieveWords()
    {
        // make sure to remove any accidental space and normalize to lower case
        return File.ReadLines(FileName).Select(l => l.Trim().ToLower());
    }
} 

Then, we can create an anagram finder to create the lookup on construction, and then find words on demand:

public class AnagramFinder
{
    private readonly ILookup<string, string> _anagramLookup; 

    public AnagramFinder(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)));
    }

    public IList<string> FindAnagrams(string word)
    {
        // at lookup time, sort the input word as the key,
        // and return all words (minus the input word) in the sequence
        string input = word.ToLower();
        string key = string.Concat(input.OrderBy(c => c));

        return _anagramLookup[key].Where(w => w != input).ToList();
    }
}

Notice the ToLookup() extension method (in System.Linq), this method creates an instance of Lookupfrom 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 key to list of values” that, when you query using the “[…]” operator, will return an IEnumerable<T> of the values, or empty sequence if the key was 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”

时间: 2024-10-23 23:46:49

Little Puzzlers–List All Anagrams in a Word的相关文章

LeetCode[Hash Table]: Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:对每一个单词的所有字母按照字典顺序排序,排序结果作为key,所有具有相同key的单词组合在一起成为一个Anagram group.最后返回所有的Anagram group. class Solution { public: vector<string> anag

12、Anagrams by Stack

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] where i stands for Push and o stands for Pop. Your program should, given pairs

438. Find All Anagrams in a String

Problem statement Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of outpu

[LeetCode]题解(python):049-Groups Anagrams

题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

Anagrams leetcode java

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 这道题看所给的字符串数组里面有多少个是同一个变形词变的.这道题同样使用HashMap来帮助存老值和新值,以及帮忙判断是否是变形词. 首先对每个string转换成char array然后排下序,HashMap里面的key存sort后的词,value存原始的

ZOJ Problem Set - 1004 Anagrams by Stack (回溯法)

ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i

44 道 JavaScript 难题(JavaScript Puzzlers!)

JavaScript Puzzlers原文 1. ["1", "2", "3"].map(parseInt) 答案:[1, NaN, NaN] 解析:parseInt (val, radix) :两个参数,val值,radix基数(就是多少进制转换) map 能传进回调函数 3参数 (element, index, array) parseInt('1', 0); //0代表10进制 parseInt('2', 1); //没有1进制,不合法 p

Word中 简单宏的使用

 (注意:打开文档时按住 Shift 键可以阻止 AutoOpen 宏运行) 1:Word中能够自动运行的默认宏代码名称及触发条件如下 -------------------------------------------------------- 1.名称:AutoExec 条件:启动Word或加载全局模板 2.名称:AutoNew 条件:每次生成新文档时 3.名称:AutoOpen 条件:每次打开一个已有文档时 4.名称:AutoClose 条件:每次关闭文档时 5.名称:AutoExit