Skip to content

Commit 65d0813

Browse files
add 830
1 parent bf90045 commit 65d0813

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
2526
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java) | O(n) | O(1) | |Easy|
2627
|821|[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_821.java) | O(n) | O(k) (k is the number of char C in S) | |Easy|
2728
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | O(m+n) | O(n) | |Easy| HashMap
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
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+
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++;
41+
}
42+
if ((j - i) >= 3) {
43+
result.add(Arrays.asList(i, j - 1));
44+
}
45+
i = j;
46+
}
47+
return result;
48+
}
49+
}
50+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._830;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _830Test {
13+
private static _830.Solution1 solution1;
14+
private static List<List<Integer>> expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _830.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
expected = new ArrayList<>();
24+
expected.add(Arrays.asList(3, 6));
25+
assertEquals(expected, solution1.largeGroupPositions("abbxxxxzzy"));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
expected = new ArrayList<>();
31+
assertEquals(expected, solution1.largeGroupPositions("abc"));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
expected = new ArrayList<>();
37+
expected.add(Arrays.asList(3, 5));
38+
expected.add(Arrays.asList(6, 9));
39+
expected.add(Arrays.asList(12, 14));
40+
assertEquals(expected, solution1.largeGroupPositions("abcdddeeeeaabbbcd"));
41+
}
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy