Skip to content

Commit

Permalink
Add solution and test-cases for problem 2982
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Dec 10, 2024
1 parent dbd4dda commit 1b1e2e9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# [2982.Find Longest Special Substring That Occurs Thrice II][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
# [2981.Find Longest Special Substring That Occurs Thrice I][title]

## Description
You are given a string `s` that consists of lowercase English letters.

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.

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.

A **substring** is a contiguous **non-empty** sequence of characters within a string.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Find Longest Special Substring That Occurs Thrice II
```go
```
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
```

**Example 3:**

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

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
ans := -1
cache := [26]map[int]int{}
for i := range 26 {
cache[i] = make(map[int]int)
}
l := len(s)
start := 0
cur := s[start]
for i := 1; i < l; i++ {
if s[i] == cur {
continue
}

index := cur - 'a'
count := i - start
for ll := 1; ll <= count; ll++ {
cache[index][ll] += count - ll + 1
if cache[index][ll] >= 3 && ll > ans {
ans = ll
}
}
cur = s[i]
start = i
}
index := cur - 'a'
count := l - start
for ll := 1; ll <= count; ll++ {
cache[index][ll] += count - ll + 1
if cache[index][ll] >= 3 && ll > ans {
ans = ll
}
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "aaaa", 2},
{"TestCase2", "abcdef", -1},
{"TestCase3", "abcaba", 1},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 1b1e2e9

Please sign in to comment.