|
4 | 4 | import java.util.Arrays;
|
5 | 5 | import java.util.List;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 830. Positions of Large Groups |
9 |
| - * |
10 |
| - * In a string S of lowercase letters, these letters form consecutive groups of the same character. |
11 |
| - * For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". |
12 |
| - * Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group. |
13 |
| - * The final answer should be in lexicographic order. |
14 |
| - * |
15 |
| - * Example 1: |
16 |
| - * Input: "abbxxxxzzy" |
17 |
| - * Output: [[3,6]] |
18 |
| - * Explanation: "xxxx" is the single large group with starting 3 and ending positions 6. |
19 |
| - * |
20 |
| - * Example 2: |
21 |
| - * Input: "abc" |
22 |
| - * Output: [] |
23 |
| - * Explanation: We have "a","b" and "c" but no large group. |
24 |
| - * |
25 |
| - * Example 3: |
26 |
| - * Input: "abcdddeeeeaabbbcd" |
27 |
| - * Output: [[3,5],[6,9],[12,14]] |
28 |
| - * |
29 |
| - * Note: 1 <= S.length <= 1000 |
30 |
| - */ |
31 | 7 | public class _830 {
|
32 |
| - public static class Solution1 { |
33 |
| - public List<List<Integer>> largeGroupPositions(String S) { |
34 |
| - List<List<Integer>> result = new ArrayList<>(); |
35 |
| - char[] chars = S.toCharArray(); |
36 |
| - for (int i = 0; i < chars.length; ) { |
37 |
| - char first = chars[i]; |
38 |
| - int j = i + 1; |
39 |
| - while (j < chars.length && first == chars[j]) { |
40 |
| - j++; |
| 8 | + public static class Solution1 { |
| 9 | + public List<List<Integer>> largeGroupPositions(String S) { |
| 10 | + List<List<Integer>> result = new ArrayList<>(); |
| 11 | + char[] chars = S.toCharArray(); |
| 12 | + for (int i = 0; i < chars.length; ) { |
| 13 | + char first = chars[i]; |
| 14 | + int j = i + 1; |
| 15 | + while (j < chars.length && first == chars[j]) { |
| 16 | + j++; |
| 17 | + } |
| 18 | + if ((j - i) >= 3) { |
| 19 | + result.add(Arrays.asList(i, j - 1)); |
| 20 | + } |
| 21 | + i = j; |
| 22 | + } |
| 23 | + return result; |
41 | 24 | }
|
42 |
| - if ((j - i) >= 3) { |
43 |
| - result.add(Arrays.asList(i, j - 1)); |
44 |
| - } |
45 |
| - i = j; |
46 |
| - } |
47 |
| - return result; |
48 | 25 | }
|
49 |
| - } |
50 | 26 | }
|
0 commit comments