- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathGeneralStringAlgorithms.cs
41 lines (36 loc) · 1.03 KB
/
GeneralStringAlgorithms.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
usingSystem;
namespaceAlgorithms.Strings;
/// <summary>
/// Implements simple algorithms on strings.
/// </summary>
publicstaticclassGeneralStringAlgorithms
{
/// <summary>
/// Finds character that creates longest consecutive substring with single character.
/// </summary>
/// <param name="input">String to find in.</param>
/// <returns>Tuple containing char and number of times it appeared in a row.</returns>
publicstaticTuple<char,int>FindLongestConsecutiveCharacters(stringinput)
{
varmaxChar=input[0];
varmax=1;
varcurrent=1;
for(vari=1;i<input.Length;i++)
{
if(input[i]==input[i-1])
{
current++;
if(current>max)
{
max=current;
maxChar=input[i];
}
}
else
{
current=1;
}
}
returnnewTuple<char,int>(maxChar,max);
}
}