# C#LeetCode刷题之#830-较大分组的位置（Positions of Large Groups）

In a string S of lowercase letters, these letters form consecutive groups of the same character.

For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”.

Call a group large if it has 3 or more characters.  We would like the starting and ending positions of every large group.

The final answer should be in lexicographic order.

Input: “abbxxxxzzy”

Output: [[3,6]]

Explanation: “xxxx” is the single large group with starting  3 and ending positions 6.

Input: “abc”

Output: []

Explanation: We have “a”,”b” and “c” but no large group.

Input: “abcdddeeeeaabbbcd”

Output: [[3,5],[6,9],[12,14]]

Note:  1 <= S.length <= 1000

```public class Program {

public static void Main(string[] args) {
string S = string.Empty;

S = "abbxxxxzzy";
var res = LargeGroupPositions(S);
ShowArray(res);

S = "abcdddeeeeaabbbcd";
res = LargeGroupPositions2(S);
ShowArray(res);

}

private static void ShowArray(IList<IList<int>> array) {
foreach(var list in array) {
foreach(var index in list) {
Console.Write(\$"{index} ");
}
}
Console.WriteLine();
}

private static IList<IList<int>> LargeGroupPositions(string S) {
var result = new List<IList<int>>();
var last = '\0';
var startIndex = -1;
var endIndex = -1;
for(var i = 0; i < S.Length; i++) {
if(S[i] != last || i == S.Length - 1) {
endIndex = i - 1;
if(i == S.Length - 1 && S[i] == last) endIndex = i;
if(endIndex - startIndex + 1 >= 3) {
var item = new List<int>();
}
startIndex = i;
}
last = S[i];
}
return result;
}

private static IList<IList<int>> LargeGroupPositions2(string S) {
var result = new List<IList<int>>();
for(var i = 0; i < S.Length; i++) {
var next = i + 1;
var dic = new Dictionary<int, int>();
while(next < S.Length && S[next] == S[i]) {
if(next - i >= 2) {
dic[i] = next;
}
next++;
}
if(dic.TryGetValue(i, out int value)) {
result.Add(new int[] { i, value });
i = next - 1;
//循环内部更改循环计数是一种不好的做法，切记切记
//如果不是必需，请勿模仿此做法
}
}
return result;
}

}```

```3 6
3 5 6 9 12 14```