Skip to content

Commit 0fd7fe0

Browse files
authored
Merge pull request #1058 from 0xff-dev/2982
Add solution and test-cases for problem 2982
2 parents f74b972 + 1b1e2e9 commit 0fd7fe0

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

leetcode/2901-3000/2982.Find-Longest-Special-Substring-That-Occurs-Thrice-II/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
1-
# [2982.Find Longest Special Substring That Occurs Thrice II][title]
2-
3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
1+
# [2981.Find Longest Special Substring That Occurs Thrice I][title]
52

63
## Description
4+
You are given a string `s` that consists of lowercase English letters.
5+
6+
A string is called **special** if it is made up of only a single character. For example, the string `"abc"` is not special, whereas the strings `"ddd"`, `"zz"`, and `"f"` are special.
7+
8+
Return the length of the **longest special substring** of `s` which occurs **at least thrice**, or `-1` if no special substring occurs at least thrice.
9+
10+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: s = "aaaa"
16+
Output: 2
17+
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
18+
It can be shown that the maximum length achievable is 2.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Find Longest Special Substring That Occurs Thrice II
23-
```go
2423
```
24+
Input: s = "abcdef"
25+
Output: -1
26+
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
27+
```
28+
29+
**Example 3:**
2530

31+
```
32+
Input: s = "abcaba"
33+
Output: 1
34+
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
35+
It can be shown that the maximum length achievable is 1.
36+
```
2637

2738
## 结语
2839

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
ans := -1
5+
cache := [26]map[int]int{}
6+
for i := range 26 {
7+
cache[i] = make(map[int]int)
8+
}
9+
l := len(s)
10+
start := 0
11+
cur := s[start]
12+
for i := 1; i < l; i++ {
13+
if s[i] == cur {
14+
continue
15+
}
16+
17+
index := cur - 'a'
18+
count := i - start
19+
for ll := 1; ll <= count; ll++ {
20+
cache[index][ll] += count - ll + 1
21+
if cache[index][ll] >= 3 && ll > ans {
22+
ans = ll
23+
}
24+
}
25+
cur = s[i]
26+
start = i
27+
}
28+
index := cur - 'a'
29+
count := l - start
30+
for ll := 1; ll <= count; ll++ {
31+
cache[index][ll] += count - ll + 1
32+
if cache[index][ll] >= 3 && ll > ans {
33+
ans = ll
34+
}
35+
}
36+
37+
return ans
538
}

leetcode/2901-3000/2982.Find-Longest-Special-Substring-That-Occurs-Thrice-II/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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+
{"TestCase1", "aaaa", 2},
17+
{"TestCase2", "abcdef", -1},
18+
{"TestCase3", "abcaba", 1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)