Created
May 31, 2013 18:51
-
-
Save serkansendur/5687092 to your computer and use it in GitHub Desktop.
get permutations of a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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