Ctrl K

Longest Alphabet Run

Medium
Call a string *alphabetical* if each of its letters comes right after the previous one in the alphabet, like cde or pqrst. Strings such as ace, dc or za are not alphabetical: the alphabet does not wrap around after z. Given a string text of lowercase English letters, return the length of its longest substring (contiguous piece) that is alphabetical. A single letter on its own is an alphabetical string of length 1.
Example 1
text="xyzabcdmn" → 4

The runs of consecutive letters are xyz (3), abcd (4) and mn (2). The longest is abcd. The alphabet does not wrap after z, so za does not count.

Example 2
text="kkk" → 1

Equal letters are not neighbours in the alphabet, so every run is a single letter.

Example 3
text="fghij" → 5
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumLongest Mirror Fragment MediumReading a Zigzag by Rows MediumLenient Number Reader MediumNumber in Roman Numerals Medium
loading Python…
Input
text="xyzabcdmn"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.