4

Possible Duplicate:
Generate list of all possible permutations of a string

I need to work on a small algorithm project where I need to be able to list and write to a text file the possible combination of a given set of characters based on a given limit.

For example, if I enter the characters "a", "b", and "c" and set the limit to 3 the possible output would be:

a
b
c
aa
bb
cc
ab
ac
ba
bc
ca
cb
...
    aaa
    bbb
    ccc
    ...
    ...
    abc
    acb
    bac
    bca
    cab
    cba

Until all possible combinations have been devised.

Writing this to a text file is no problem with me. Having an algorithm that would write the combination is something that I am quite not good at.

Appreciate in .NET (C# or VB) codes.

Thanks.

PS

On a side note, I wonder how long it would take for an application to create a string combination of all possible keyboard characters and how big the file would get to be.

Updated: I should also show character combination from n characters to the implied limit..

peterh
  • 11,875
  • 18
  • 85
  • 108
Batuta
  • 1,684
  • 17
  • 48
  • 62
  • quick google search for "character combination algorithm" found this discussion (this was the first hit in a list of other good-looking candidates) where they outline an algorithm: http://www.daniweb.com/forums/thread110604.html – FrustratedWithFormsDesigner Dec 14 '10 at 20:14
  • There are `n**b` unique permutations of `n` items from a set of size `b`. (in particular, this tells you how many numbers n digits in base b can represent (e.g. 5 bits: `2**5 == 32`)). Even assuming ASCII (95 printable chars), there are 857,375 3-letter, 81,450,625 4-letter, (and so on) strings. This grows expotentially. And nowadaws, you have codepages with 256 chars or even Unicode. For many purposes, there are much cheaper solutions... –  Dec 14 '10 at 20:18

3 Answers3

3

See Generate list of all possible permutations of a string

Community
  • 1
  • 1
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
1

Well, for your example, I might try something like this.

string chars = "abc";

for (int a = 0; a < chars.Length; a++)
{
    for (int b = 0; b < chars.Length; b++)
    {
        for (int c = 0; c < chars.Length; c++)
        {
            string row = String.Format("{0}{1}{2}", chars[a], chars[b], chars[c]);
        }
    }
}

That's just typed here so it may contain errors. Also, I'm not sure if the character limit is tied to the number of possible characters. But perhaps this will give you a starting point.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

You can enumerate all the permutations in a string using a recursive implementation. A quick but functional implementation might look like the following:

Edit: You changed the OP to include strings with length less than the input character set. The following code has been modified. It gives exactly the output in your question.

static void BuildPermutations(string input, char[] current, int index, int depth, List<string> perms)
{
    if (index == depth)
    {
        perms.Add(new string(current, 0, depth));
        return;
    }
    for (int n = 0; n < input.Length; ++n)
    {
        current[index] = input[n];
        BuildPermutations(input, current, index + 1, depth, perms);
    }
}


static void Main(string[] args)
{
    string input = "abc";
    char[] current = new char[input.Length];
    List<string> perms = new List<string>();
    for (int n = 1; n <= 3; ++n )
        BuildPermutations(input, current, 0, n, perms);
    foreach (string s in perms)
        System.Console.WriteLine(s.ToString());
}
DeusAduro
  • 5,971
  • 5
  • 29
  • 36