I have a string and I want to record the char repeating times. Let me use examples.
string s = "22";
||
"12";// I wanted, the first char '2' occurred, record as 1, the second occurred ,record as 2 because of same.
string s = "2322";
||||
"1112";
string s = "23222222";
||||||||
"11123456";
What I think is to use a dictionary maybe...
My partial code, please correct me if wrong.
public Dictionary<int, int> GetCharRepeatingTimesByIndex(string input)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
dict[0] = 0;
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length - 1; i++)
{
dict[i+1] = 0;
if (c[i + 1] != c[i])
{
dict[i + 1] = dict[i] + 1;
}
}
return dict;
}
No comments:
Post a Comment