8000 :sparkles: Added solution to 830 · ninefive/awesome-golang-leetcode@74cf4bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 74cf4bf

Browse files
author
Sathish Babu
committed
✨ Added solution to 830
1 parent f099aa0 commit 74cf4bf

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/0830.Positions-of-Large-Groups/README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,35 @@
55
66
## Description
77

8+
In a string S of lowercase letters, these letters form consecutive groups of the same character.
9+
10+
For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy".
11+
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+
14+
The final answer should be in lexicographic order.
15+
816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: "abbxxxxzzy"
20+
Output: [[3,6]]
21+
Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: "abc"
28+
Output: []
29+
Explanation: We have "a","b" and "c" but no large group.
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: "abcdddeeeeaabbbcd"
36+
Output: [[3,5],[6,9],[12,14]]
1337
```
1438

1539
## 题意
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(S string) [][]int {
4+
n := len(S)
5+
if n < 3 {
6+
return [][]int{}
7+
}
8+
res := make([][]int, 0)
9+
c, l, r := 1, -1, -1
10+
for i := 1; i < n; i++ {
11+
if S[i] == S[i-1] {
12+
c++
13+
if l == -1 {
14+
l = i - 1
15+
}
16+
if c >= 3 {
17+
r = i
18+
}
19+
} else {
20+
if c >= 3 {
21+
res = append(res, []int{l, r})
22+
}
23+
c, l = 1, -1
24+
}
25+
}
26+
if c >= 3 {
27+
res = append(res, []int{l, r})
28+
}
29+
return res
530
}

src/0830.Positions-of-Large-Groups/Solution_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", "abbxxxxzzy", [][]int{{3, 6}}},
17+
{"TestCase", "abcdddeeeeaabbbcd", [][]int{{3, 5}, {6, 9}, {12, 14}}},
18+
{"TestCase", "ab", [][]int{}},
19+
{"TestCase", "xxxaaaa", [][]int{{0, 2}, {3, 6}}},
1920
}
2021

2122
// 开始测试

0 commit comments

Comments
 (0)
0