What's a good way for figuring out all possible words of a given length

后端 未结 12 1777
一生所求
一生所求 2021-02-04 17:27

I\'m trying to create an algorithm in C# which produces the following output strings:

AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ

What is the

12条回答
  •  孤街浪徒
    2021-02-04 18:10

    Just a coment to Garry Shutler, but I want code coloring:

    You really don't need to make it IQuaryable, neither the sort, so you can remove the second method. One step forwad is to use Aggregate for the cross product, it end up like this:

    IEnumerable letters = new[]{
                    "A","B","C","D","E","F",                       
                    "G","H","I","J","K","L",
                    "M","N","O","P","Q","R","S",           
                    "T","U","V","W","X","Y","Z"};
    
    var result = Enumerable.Range(0, 4)
                    .Aggregate(letters, (curr, i) => curr.SelectMany(s => letters, (s, c) => s + c));
    
    foreach (var val in result)
         Console.WriteLine(val);
    

    Anders should get a Nobel prize for the Linq thing!

提交回复
热议问题