Creating a list of possible string combinations

Possible duplicate:
Generate a list of all possible line permutations

Hi guys,

I need to work on a small algorithm project where I need to be able to list and write to a text file a 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, a 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 developed.

Writing this to a text file is no problem. Having an algorithm that will write a combination is something that I don't really like.

Evaluate .NET codes (C # or VB).

Thanks.

PS

I wonder what time it took for the application to create a string combination of all possible keyboard characters and how big the file would be.

Updated: I also have to show a combination of characters from n characters to the intended limit.

+4
source share
3 answers

You can list all permutations in a string using a recursive implementation. A quick but functional implementation might look like this:

Edit:. You have modified OP to include strings shorter than the input character set. The following code has been changed. It gives exactly the result 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()); } 
+1
source

Ok, for your example, I can 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]); } } } 

It is simply printed here so that it may contain errors. In addition, I'm not sure that the character limit is related to the number of possible characters. But perhaps this will give you a starting point.

+1
source

Source: https://habr.com/ru/post/1332137/


All Articles