From f780e59e039198eabb44463ea35970240b63d40e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 10 Dec 2024 09:31:14 +0800 Subject: [PATCH] Add solution and test-cases for problem 2981 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 37 ++++++++++++++++++- .../Solution_test.go | 14 +++---- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/README.md b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/README.md index dea27366..a96dc9f0 100755 --- a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/README.md +++ b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/README.md @@ -1,28 +1,39 @@ # [2981.Find Longest Special Substring That Occurs Thrice I][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) - ## 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 I -```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. +``` ## 结语 diff --git a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution.go b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution.go index d115ccf5..a65264e6 100644 --- a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution.go +++ b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution.go @@ -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 } diff --git a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution_test.go b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution_test.go index 14ff50eb..5a0ff5a4 100644 --- a/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution_test.go +++ b/leetcode/2901-3000/2981.Find-Longest-Special-Substring-That-Occurs-Thrice-I/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }