Length of the Longest Alphabetical Continuous Substring - Problem

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.

Input & Output

Example 1 — Basic Continuous Sequence
$ Input: s = "abacabad"
Output: 2
💡 Note: The longest alphabetical continuous substring is "ab" which has length 2
Example 2 — Multiple Sequences
$ Input: s = "abcde"
Output: 5
💡 Note: The entire string "abcde" is alphabetically continuous with length 5
Example 3 — Single Character
$ Input: s = "a"
Output: 1
💡 Note: Single character strings have alphabetical continuous length of 1

Constraints

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

Visualization

Tap to expand
Find Longest Alphabetical Continuous SubstringInput: "abcxdefgh" → Find longest consecutive alphabet sequenceabcxdefg"abc" (length 3)"defg" (length 4)Breaks continuity!Compare sequences: max(3, 4) = 4Output: 4
Understanding the Visualization
1
Input String
String with mixed alphabetical sequences
2
Find Sequences
Identify consecutive alphabet letter groups
3
Return Length
Length of the longest continuous sequence
Key Takeaway
🎯 Key Insight: Track current sequence length, reset when alphabetical continuity breaks
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen