Make permutation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Permutation2
{
class Program
{
private static void permutation(string str)
{
int length = str.Length;
bool[] used = new bool[length];

StringBuilder sb = new StringBuilder();
char[] arrChar = str.ToCharArray();

doPermute(arrChar, sb, used, length, 0);
}

private static void doPermute(char[] arrCh, StringBuilder sb, bool[] used, int length, int level)
{
if (level == length)
{
Console.WriteLine(sb.ToString());
return;
}

for (int i = 0; i < length; ++i)
{
if (used[i])
continue;

sb.Append(arrCh[i]);
used[i] = true;
doPermute(arrCh, sb, used, length, level + 1);
used[i] = false;
//sb.Capacity = sb.Length - 1;
sb.Remove(sb.Length - 1, 1);
}
}

static void Main(string[] args)
{
string sample = "abc";

Console.WriteLine("{0} : ", sample);
permutation(sample);

Console.ReadLine();

}
}
}

Advertisements