I haven't enough time, but i think i'm close to finding solution using Linq...
Here is my code:
string text = "aaaaBbCcDDa";
int number = 0;
int counter = 1;
var query = from letters in text.ToArray()
select new { index = number++, letter = letters, prev = text[number - 2 < 0 ? 0 : number - 2], countNo = letters == text[number - 2 < 0 ? 0 : number - 2] ? counter++ : counter=1 };
Console.WriteLine("Index | Letter | Prev | Count");
foreach (var v in query)
{
Console.WriteLine(" {0} | {1} | {2} | {3}", v.index, v.letter, v.prev, v.countNo );
}
Console.ReadKey();
Result:
index letter prev countNo
0 a a 1
1 a a 2
2 a a 3
3 a a 4
4 B a 1
5 b B 1
6 C b 1
7 c C 1
8 D c 1
9 D D 1 --> here should be 2, that's why i wrote that i almost found solution ;)
10 a D 1
To do: improve linq query, then write another query to get max of
countNo
for each
letter
;)