-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecurseFactorial.cs
84 lines (77 loc) · 2.83 KB
/
RecurseFactorial.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms
{
internal class RecurseFactorialAndNot
{
internal static void Run()
{
// make all string variants from character set
//MakeAllStringsFactorial("");
//MakeAllStringsFactorial("a");
//MakeAllStringsFactorial("ab");
MakeAllStringsFactorial("abcd"); // abcd dcba cbad ...
//MakeAllStringsFactorial("ab cdc ey dfxcskq");
MakeAllStringsByOrder("abcd"); // abcd cd bc bcd a...
MakeAllStringsByOrderWithGap("abcd"); // abcd cd bc bcd a ac abd ...
}
private static void MakeAllStringsByOrderWithGap(string str)
{
var res = new List<string>();
MakeAllStringsByOrderRecurseWithGap(str, res);
Console.WriteLine("For " + str + ", count = " + res.Count);
}
private static void MakeAllStringsByOrderRecurseWithGap(string str, List<string> res, string acc = "")
{
if(str.Length == 0)
{
if(acc.Length != 0)
res.Add(acc);
return;
}
char c = str[0];
MakeAllStringsByOrderRecurseWithGap(str.Substring(1, str.Length - 1), res, acc + c);
MakeAllStringsByOrderRecurseWithGap(str.Substring(1, str.Length - 1), res, acc);
}
private static void MakeAllStringsByOrder(string str)
{
var res = new List<string>();
MakeAllStringsByOrder(str, res);
Console.WriteLine("For " + str + ", count = " + res.Count);
}
private static void MakeAllStringsByOrder(string str, List<string> res)
{
for (int i=0; i < str.Length; i++)
{
for (int ii = 0; ii < str.Length - i; ii++)
{
var ss = str.Substring(i, ii + 1); // refactor it to Span using
res.Add(new string(ss));
}
}
}
private static void MakeAllStringsFactorial(string str)
{
var res = new List<string>();
MakeAllStringsRecurse(str.Distinct().Where(c => char.IsLetter(c)).ToList(), res);
Console.WriteLine("For " + str + ", count = " + res.Count);
}
private static void MakeAllStringsRecurse(List<char> str, List<string> res, string acc = "")
{
if (str.Count == 0)
//if(acc.Length == 2) // only 2char strings
{
res.Add(acc);
return;
}
foreach (char c in str)
{
MakeAllStringsRecurse(str.Where(ch => ch != c).ToList(), res, acc + c);
}
}
}
}