public static bool IsAnagramOf(this string word1, string word2)
{
    return word1.OrderBy(x => x).SequenceEqual(word2.OrderBy(x => x));
}

I'm currently pulling everything from a large xml file with all english words. I'm then comparing each word against the given string to see if it's an anagram. I'm then storing each correct word and returning them.

However...

I'm wanting to make it so the anagrams do not have to be of equal string length.

For example: "Hello" contains "Hello", "Hell", "He" etc...

Is there anyway to do this that's relatively small in code?

Thanks!

Edit: So including subanagrams as well as anagrams of equal length.

有帮助吗?

解决方案

Maybe your method should be called ContainsTheSameSetOfLetters?

public static bool ContainsTheSameSetOfLetters(this string word1, string word2)
{
    var chars = new HashSet<char>(word1);
    return word2.All(x => chars.Contains(x));
}

If you care about number of time particular letter is being used, you can use following:

public static bool ContainsTheSameSetOfLetters(string word1, string word2)
{
    var chars = word1.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
    return word2.GroupBy(x => x).All(g => chars.ContainsKey(g.Key) && chars[g.Key] >= g.Count());
}

其他提示

Instead of using SequenceEqual, try creating an extension method that checks that the sequence starts with another sequence.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top