-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (65 loc) · 2.08 KB
/
Program.cs
File metadata and controls
70 lines (65 loc) · 2.08 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataStructuresAndAlgorithmPractise
{
class Program
{
Dictionary<char, int> letterFrequency = new Dictionary<char, int>();
int sLenth;
public int UniqueLetterString(string s)
{
char[] charArray = s.ToCharArray();
sLenth = charArray.Length;
NumOfIndistinctChars(charArray);
return NumberOfUniqueChars(charArray);
}
private void NumOfIndistinctChars(char[] charArray)
{
for (int i= 0; i<charArray.Length; i++)
{
if (letterFrequency.ContainsKey(charArray[i]))
{
letterFrequency[charArray[i]]++;
}
else
{
letterFrequency.Add(charArray[i], 1);
}
}
}
private int Factoriel(int n)
{
int result=1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
public int NumberOfUniqueChars(char[] charArray)
{
int totalCount = 0;
for (int i = charArray.Length; i >=1 ; i--)
{
totalCount = (Convert.ToInt32(Math.Pow(i, 2)) + i) / 2;
}
foreach (var item in letterFrequency.Where(p => p.Value>1))
{
totalCount -= (Factoriel(sLenth) / (Factoriel(item.Value) * Factoriel(sLenth - item.Value))) * ((sLenth - item.Value) / sLenth);
}
return totalCount;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int number; //= UniqueLetterString("ABA");
//Console.WriteLine(number.ToString());
//number = UniqueLetterString("ABC");
//Console.WriteLine(number.ToString());
//number = UniqueLetterString("LEETCODE");
//Console.WriteLine(number.ToString());
}
}
}