-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringPermutation.cs
51 lines (44 loc) · 1.09 KB
/
StringPermutation.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
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
namespace ConsoleApplication21
{
internal static class Program
{
public static void Main(string[] args)
{
const string s = "ABCDEF";
var arr = s.ToCharArray();
GetPermutation(arr);
}
private static void GetPermutation(char[] arr)
{
var x = arr.Length - 1;
GetPer(arr, 0, x);
}
private static void GetPer(char[] arr, int k, int m)
{
if (k == m)
Console.WriteLine(arr);
else
{
for (var i = k; i <= m; i++)
{
Swap(ref arr[k], ref arr[i]);
GetPer(arr, k + 1, m);
Swap(ref arr[k], ref arr[i]);
}
}
}
private static void Swap(ref char c, ref char c1)
{
if (c == c1)
{
return;
}
c ^= c1;
c1 ^= c;
c ^= c1;
}
}
}