1
class Solution {
2
public List<List<Integer>> largeGroupPositions(String s) {
3
List<List<Integer>> res = new ArrayList<>();
4
List<Integer> tmp = new ArrayList<>();
5
int count = 1;
6

7
for (int i = 0; i < s.length() - 1; i++) {
8
// Increment the count until the next element is the same as the previous element. Ex: "aaa"
9
if (s.charAt(i) == s.charAt(i + 1)) {
10
count++;
11
}
12
// Add the first and last indices of the substring to the list when the next element is
13
// different from the previous element. Ex: "aaab"
14
else if (s.charAt(i) != s.charAt(i + 1) && count >= 3) {
15
// gives the starting index of substring
16
tmp.add(i - count + 1);
17
// gives the last index of substring
18
tmp.add(i);
19
res.add(tmp);
20
count = 1;
21
tmp = new ArrayList<>();
22
} else {
23
count = 1;
24
}
25
}
26

27
// Check for a large group at the end of the string. Ex: "abbb".
28
if (count >= 3) {
29
tmp.add(s.length() - count);
30
tmp.add(s.length() - 1);
31
res.add(tmp);
32
}
33

34
return res;
35
}
36
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0