c# - How to get all the possible 3 letter permutations? -
possible duplicate:
listing permutations of string/integer
for example,
aaa .. aaz .. aba .. abz .. aca .. acz .. azz .. baa .. baz .. bba .. bbz .. zzz
basically, imagine counting binary instead of going 0 1, goes z.
i have been trying working few hours no avail , formula getting quite complex , i'm not sure if there's simpler way it.
thanks reading.
edit: have @ moment it's not quite there , i'm not sure if there better way:
private ienumerable<string> getwordsoflength(int length) { char lettera = 'a', letterz = 'z'; stringbuilder currentletters = new stringbuilder(new string(lettera, length)); stringbuilder endingletters = new stringbuilder(new string(letterz, length)); int currentindex = length - 1; while (currentletters.tostring() != endingletters.tostring()) { yield return currentletters.tostring(); (int = length - 1; > 0; i--) { if (currentletters[i] == letterz) { (int j = i; j < length; j++) { currentletters[j] = lettera; } if (currentletters[i - 1] != letterz) { currentletters[i - 1]++; } } else { currentletters[i]++; break; } } } }
for variable amount of letter combinations, can following:
var alphabet = "abcdefghijklmnopqrstuvwxyz"; var q = alphabet.select(x => x.tostring()); int size = 4; (int = 0; < size - 1; i++) q = q.selectmany(x => alphabet, (x, y) => x + y); foreach (var item in q) console.writeline(item);
Comments
Post a Comment