简体   繁体   中英

How to generate all possible 3-chars strings in c#?

The question is :

How to generate a list of all possible 3-chars strings in C#?

To clairify, that's within the domain of 64 10 and 90 10

IEnumerable<string> GetAllStrings(params char[] inputCharacterSet) {
    return from n in inputCharacterSet
           from m in inputCharacterSet
           from k in inputCharacterSet
           select new string(new [] { n, m, k });
}
public IEnumerable<String> Get3CharStrings(char[] domain)
{
    foreach(char a in domain)
     foreach(char b in domain)
      foreach(char c in domain)
       yield return "" + a + b + c;
}

This is actually quite a bit slower than the LINQ solution posted by Mehrdad, although most of the difference lies in the use of return "" + a + b + c instead of return new string(new[] { a, b, c}) . 这实际上比Mehrdad发布的LINQ解决方案要慢很多,尽管大多数区别在于使用return "" + a + b + c而不是return new string(new[] { a, b, c})

Actual statistics (26-character alphabet, 10k iterations:

Mehrdad's code: 72.983 seconds
My code: 127.205 seconds
My code with Mehrdad's return statement: 75.055 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM