Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72b71c5

Browse files
committed
Add: Positions of Large Groups
1 parent 10dadcc commit 72b71c5

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
package positions_of_large_groups
2+
3+
func largeGroupPositions(S string) [][]int {
4+
pre, ans, l := 0, make([][]int, 0), len(S)
5+
for i, v := range S {
6+
if i == l-1 || v != rune(S[i+1]) {
7+
if i-pre >= 2 {
8+
ans = append(ans, []int{pre, i})
9+
}
10+
pre = i + 1
11+
}
12+
}
13+
return ans
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
11
package positions_of_large_groups
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input string
10+
expected [][]int
11+
}
12+
13+
func TestLargeGroupPositions(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: "abbxxxxzzy",
17+
expected: [][]int{
18+
{3, 6},
19+
},
20+
},
21+
{
22+
input: "abc",
23+
expected: [][]int{},
24+
},
25+
{
26+
input: "abcdddeeeeaabbbcd",
27+
expected: [][]int{
28+
{3, 5},
29+
{6, 9},
30+
{12, 14},
31+
},
32+
},
33+
{
34+
input: "abcddd",
35+
expected: [][]int{
36+
{3, 5},
37+
},
38+
},
39+
}
40+
for _, tc := range tests {
41+
output := largeGroupPositions(tc.input)
42+
if !reflect.DeepEqual(output, tc.expected) {
43+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
44+
}
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /