Last active
August 29, 2015 14:26
-
-
Save iterativo/8c8f58da086a9edf58f3 to your computer and use it in GitHub Desktop.
String Permutation in C#
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
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