2011-09-23 41 views
1

我有问题计算包含多个字母实例的字符串(例如“HRWSOROE”其中'O'和'R'两次在字符串中的排列组合我使用的C# - 查找字符串中的所有字母重复的字母

public static class Extensions 
{ 
    public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source) 
    { 
     if (source == null) throw new ArgumentNullException("source"); 
     return PermutationsImpl(source, Enumerable.Empty<T>()); 
    } 

    private static IEnumerable<IEnumerable<T>> PermutationsImpl<T>(IEnumerable<T> source, IEnumerable<T> prefix) 
    { 
     if (!source.Any()) yield return prefix; 
     foreach (var permutation in source.SelectMany(x => PermutationsImpl(source.Except(Yield(x)), prefix.Union(Yield(x))))) 
      yield return permutation; 
    } 

    private static IEnumerable<T> Yield<T>(this T element) 
    { 
     yield return element; 
    } 
} 

算法似乎工作,但忽略重复的字母 - 这样反而对“HRWSOROE”正在对“HRWSOE”计算的排列计算排列如果有人会好心地回顾一下。我已经让我知道他们在想什么,我会很感激。

谢谢!

+2

Aaaah ...有人写一个Wordfeud“骗子”......;) –

+2

用这些字母,你可以打85分的HORSEROW。 – bzlm

+0

是的,负责。将它整合到一个整洁的移动应用程序中会相当酷。 –

回答

2

这里的问题似乎是PermutationsImpl()中LINQ行中的源代码(Yield(x))部分。

它正在比较和删除与'x'中的值匹配的源中的所有值。

相关问题