2011-09-23 37 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'中的值匹配的源中的所有值。

相關問題