例如"aabbccdeeef",其中eee最长,返回第一个E的位置,应该为7.
View Code
static int FindStartLargestRepeat(string str) { if (string.IsNullOrEmpty(str)) { throw new Exception("Input can't be empty."); } int count = 1; int maxCount = 1; int startIndex = 0; for (int i = 1; i < str.Length; i++) { if (str[i]==str[i-1]) { count++; if (count>maxCount) { maxCount = count; startIndex = i - maxCount + 1; } } else { count = 1; } } return startIndex; }