-
Notifications
You must be signed in to change notification settings - Fork 0
/
830. Positions of Large Groups.java
43 lines (38 loc) · 1.11 KB
/
830. Positions of Large Groups.java
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
42
43
import java.util.ArrayList;
import java.util.Arrays;
class Solution {
public List<List<Integer>> largeGroupPositions(String S) {
List<List<Integer>> result = new ArrayList();
if (S.length() < 3)
return result;
char c = S.charAt(0);
int len = S.length();
int num = 1;
for (int i = 1; i < len; i++) {
if (S.charAt(i) == c)
num++;
else {
if (num >= 3) {
result.add(Arrays.asList(new Integer[] { i - num, i - 1 }));
}
num = 1;
c = S.charAt(i);
}
}
if (num >= 3) {
result.add(Arrays.asList(new Integer[] { len - num, len - 1 }));
}
return result;
}
}
// List<List<Integer>> ans = new ArrayList();
// int i = 0, N = S.length(); // i is the start of each group
// for (int j = 0; j < N; ++j) {
// if (j == N-1 || S.charAt(j) != S.charAt(j+1)) {
// // Here, [i, j] represents a group.
// if (j-i+1 >= 3)
// ans.add(Arrays.asList(new Integer[]{i, j}));
// i = j + 1;
// }
// }
// return ans;