如何获得所有可能的3个字母的排列? [英] How to get all the possible 3 letter permutations?

查看:119
本文介绍了如何获得所有可能的3个字母的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer">Listing字符串/整数的所有排列

例如,

  AAA .. AAZ .. ABA .. ABZ .. ACA .. ACZ .. AZZ ..咩..巴兹.. BBA .. BBZ .. ZZZ
 

基本上,想象二进制计数但不是从0到1,它从A到Z。

我一直在试图得到这个工作了几个小时,现在没有用的公式得到相当复杂,我不知道是否有一种简单的方法来做到这一点。

感谢您的阅读。

编辑:我有这样的事情的时刻,但它并不完全那里,我不知道是否有更好的方法:

 私人的IEnumerable&LT;字符串&GT; GetWordsOfLength(INT长度)
{
    焦炭letterA ='A',letterZ ='Z';

    StringBuilder的currentLetters =新的StringBuilder(新的字符串(letterA,长度));
    StringBuilder的endingLetters =新的StringBuilder(新的字符串(letterZ,长度));

    INT CURRENTINDEX =长度 -  1;

    而(currentLetters.ToString()!= endingLetters.ToString())
    {
        得到回报currentLetters.ToString();

        的for(int i =长度 -  1; I&GT; 0;我 - )
        {
            如果(currentLetters [我] == letterZ)
            {
                对于(INT J =; J&LT;长度; J ++)
                {
                    currentLetters [J] = letterA;
                }

                如果(currentLetters [我 -  1]!= letterZ)
                {
                    currentLetters [我 -  1] ++;
                }
            }
            其他
            {
                currentLetters [Ⅰ] ++;

                打破;
            }
        }
    }
}
 

解决方案

有关字母组合的变量数量,你可以做到以下几点:

  VAR字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
变种Q = alphabet.Select(X =&GT; x.ToString());
INT大小= 4;
的for(int i = 0; I&LT;尺寸 -  1;我++)
    Q = q.SelectMany(X =&GT;字母表,(X,Y)=&GT; X + Y);

的foreach(在Q VAR项)
    Console.WriteLine(项目);
 

Possible Duplicate:
Listing all permutations of a string/integer

For example,

aaa .. aaz .. aba .. abz .. aca .. acz .. azz .. baa .. baz .. bba .. bbz .. zzz

Basically, imagine counting binary but instead of going from 0 to 1, it goes from a to z.

I have been trying to get this working for a few hours now to no avail and the formula is getting quite complex and I'm not sure if there's a simpler way to do it.

Thanks for reading.

Edit: I have something like this at the moment but it's not quite there and I'm not sure if there is a 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();

        for (int i = length - 1; i > 0; i--)
        {
            if (currentLetters[i] == letterZ)
            {
                for (int j = i; j < length; j++)
                {
                    currentLetters[j] = letterA;
                }

                if (currentLetters[i - 1] != letterZ)
                {
                    currentLetters[i - 1]++;
                }
            }
            else
            {
                currentLetters[i]++;

                break;
            }
        }
    }
}

解决方案

For a variable amount of letter combinations, you can do the following:

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var q = alphabet.Select(x => x.ToString());
int size = 4;
for (int i = 0; i < size - 1; i++)
    q = q.SelectMany(x => alphabet, (x, y) => x + y);

foreach (var item in q)
    Console.WriteLine(item);

这篇关于如何获得所有可能的3个字母的排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆