-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPositionsOfLargeGroups.java
39 lines (33 loc) · 1 KB
/
PositionsOfLargeGroups.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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// https://leetcode.com/problems/positions-of-large-groups/
public class PositionsOfLargeGroups {
private final String input;
public PositionsOfLargeGroups(String input) {
this.input = input;
}
public List<List<Integer>> solution() {
List<List<Integer>> result = new ArrayList<>();
char prev = input.charAt(0);
int count = 1;
int length = input.length();
for (int i = 1; i < length; i++) {
char c = input.charAt(i);
if (c == prev) {
count++;
} else {
if (count >= 3) {
result.add(Arrays.asList(i - count, i - 1));
}
count = 1;
prev = c;
}
}
if (count >= 3) {
result.add(Arrays.asList(length - count, length - 1));
}
return result;
}
}