-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocationOfLargerGroupings.java
More file actions
72 lines (44 loc) · 1.52 KB
/
LocationOfLargerGroupings.java
File metadata and controls
72 lines (44 loc) · 1.52 KB
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
44
45
46
47
package array_and_matrix;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。 例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", "bb", "xxxx", "z" 和 "yy" 这样的一些分组。
* <p>
* 我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。
* <p>
* 最终结果按照字典顺序输出。
* <p>
* 示例 1:
* <p>
* 输入: "abbxxxxzzy"
* 输出: [[3,6]]
* 解释: "xxxx" 是一个起始于 3 且终止于 6 的较大分组。
* 示例 2:
* <p>
* 输入: "abc"
* 输出: []
* 解释: "a","b" 和 "c" 均不是符合要求的较大分组。
* 示例 3:
* <p>
* 输入: "abcdddeeeeaabbbcd"
* 输出: [[3,5],[6,9],[12,14]]
* @Date: Created in 9:34 6/5/2020
* @Modified by:
*/
public class LocationOfLargerGroupings {
public List<List<Integer>> largeGroupPositions(String S) {
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;
}
}