Skip to content

Instantly share code, notes, and snippets.

@serkansendur
Created May 31, 2013 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serkansendur/5687092 to your computer and use it in GitHub Desktop.
Save serkansendur/5687092 to your computer and use it in GitHub Desktop.
get permutations of a string
public static void Permute(string str)
{
int length = str.Length;
bool[] used = new bool[length];
StringBuilder output = new StringBuilder();
doPermute(str, output, used, length, 0);
}
public static void doPermute(string input, StringBuilder output,
bool[] used, int length, int level)
{
if (level == length)
{
Console.WriteLine(output.ToString());
return;
}
for(int i=0; i< length;i++)
{
if (used[i])
continue;
output.Append(input[i]);
used[i] = true;
doPermute(input, output, used, length, level + 1);
used[i] = false;
output.Length = output.Length - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment