Skip to content

Instantly share code, notes, and snippets.

@iterativo
Last active August 29, 2015 14:26
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 iterativo/8c8f58da086a9edf58f3 to your computer and use it in GitHub Desktop.
Save iterativo/8c8f58da086a9edf58f3 to your computer and use it in GitHub Desktop.
String Permutation in C#
using static System.Console;
public class StringPermuter
{
private string _word;
public StringPermuter(string word)
{
_word = word;
}
public void Permute()
{
WriteLine($"Permutations of {_word}");
Process(_word.Length);
}
private void Process(int c)
{
if (c == 1)
{
WriteLine(_word);
return;
}
for (var i = 0; i < c; i++)
{
Process(c - 1);
Rotate(c);
};
}
private void Rotate(int c)
{
var target = _word.Length - c;
_word = _word.Substring(0, target)
+ _word.Substring(target + 1)
+ _word.Substring(target, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment