Talentd
FleetCode

2414. Length of the Longest Alphabetical Continuous Substring

Medium61.0% acceptance3 companies

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: s = "abacaba"
Output: 2
Explanation: There are 4 distinct continuous substrings: "a", "b", "c" and "ab".
"ab" is the longest continuous substring.

Example 2:

Input: s = "abcde"
Output: 5
Explanation: "abcde" is the longest continuous substring.

Constraints:

  • 1 <= s.length <= 105
  • s consists of only English lowercase letters.

Asked at 3 companies

Topics

Hints open one at a time

Hint 1
What is the longest possible continuous substring?
Hint 2
The size of the longest possible continuous substring is at most 26, so we can just brute force the answer.
5:27
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def longestContinuousSubstring(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abacaba"
Input
"abcde"
Your code is saved on this device.Solution