Skip to content

Commit 20bc426

Browse files
authored
Update Positions of Large Groups.java
1 parent 4a1ff57 commit 20bc426

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

Easy/Positions of Large Groups.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
class Solution {
2-
public List<List<Integer>> largeGroupPositions(String S) {
3-
List<List<Integer>> positions = new ArrayList<>();
4-
5-
int i = 1;
6-
int count = 1;
7-
int start = 0;
8-
9-
while (i < S.length()) {
10-
if (S.charAt(i) == S.charAt(i-1)) {
11-
count++;
2+
public List<List<Integer>> largeGroupPositions(String s) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
int idx = 0;
5+
while (idx < s.length()) {
6+
int startIdx = idx;
7+
char c = s.charAt(idx);
8+
while (idx < s.length() && s.charAt(idx) == c) {
9+
idx++;
1210
}
13-
else {
14-
if (count >= 3) {
15-
List<Integer> temp = Arrays.asList(start, i-1);
16-
positions.add(temp);
17-
}
18-
19-
start = i;
20-
count = 1;
11+
if (idx - startIdx >= 3) {
12+
result.add(Arrays.asList(startIdx, idx - 1));
2113
}
22-
23-
i++;
2414
}
25-
26-
if (count >= 3) {
27-
positions.add(Arrays.asList(start, i-1));
28-
}
29-
30-
return positions;
15+
return result;
3116
}
3217
}

0 commit comments

Comments
 (0)