diff --git a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/README.md b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/README.md index 0ae8f0b7f..2318eea31 100755 --- a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/README.md +++ b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/README.md @@ -1,28 +1,28 @@ # [2414.Length of the Longest Alphabetical Continuous Substring][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 +An **alphabetical continuous string** is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string `"abcdefghijklmnopqrstuvwxyz"`. + +- For example, `"abc"` is an alphabetical continuous string, while `"acb"` and `"za"` are not. + +Given a string `s` consisting of lowercase letters only, return the length of the **longest** alphabetical continuous substring. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "abacaba" +Output: 2 +Explanation: There are 4 distinct continuous substrings: "a", "b", "c" and "ab". +"ab" is the longest continuous substring. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Length of the Longest Alphabetical Continuous Substring -```go ``` - +Input: s = "abcde" +Output: 5 +Explanation: "abcde" is the longest continuous substring. +``` ## 结语 diff --git a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution.go b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution.go index d115ccf5e..b9944f825 100644 --- a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution.go +++ b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + ans := 1 + cur := 1 + for i := 1; i < len(s); i++ { + if s[i] == s[i-1]+1 { + cur++ + } else { + cur = 1 + } + ans = max(ans, cur) + } + return ans } diff --git a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution_test.go b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution_test.go index 14ff50eb4..dcb5fe271 100644 --- a/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution_test.go +++ b/leetcode/2401-2500/2414.Length-of-the-Longest-Alphabetical-Continuous-Substring/Solution_test.go @@ -10,12 +10,11 @@ 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", "abacaba", 2}, + {"TestCase2", "abcde", 5}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }