-
Notifications
You must be signed in to change notification settings - Fork 55
/
15.PermutationsOfStr.cs
55 lines (46 loc) · 1.21 KB
/
15.PermutationsOfStr.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//find all permutations of a string
using System;
using System.Collections.Generic;
namespace PermutationsOfString
{
class MainClass
{
public class Finder
{
private void swap(ref char first, ref char second)
{
if (first == second)
return;
char temp = first;
first = second;
second = temp;
}
public void FindPermutation(char[]strArray)
{
int maxDepth = strArray.Length;
this.GetPermutation (strArray, 0, maxDepth);
}
private void GetPermutation(char[] strArray, int recurisonDepth, int maxDepth)
{
int i;
if (recurisonDepth == maxDepth) {
Console.WriteLine (strArray);
Console.WriteLine ("");
} else {
for (i = recurisonDepth; i < maxDepth; i++) {
swap (ref strArray [recurisonDepth], ref strArray [i]); // try to put each element at the head, isolate each element
this.GetPermutation(strArray, recurisonDepth+1, maxDepth);
swap (ref strArray [recurisonDepth], ref strArray [i]); // restore strArray
}
}
}
}
public static void Main (string[] args)
{
Finder finder = new Finder ();
string teststr = "abcd";
char[] testchar = teststr.ToCharArray ();
finder.FindPermutation (testchar);
}
}
}