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()); }
source share