Measure the Longest Substring Advancing One Alphabet Letter at a Time

Quick Overview

Find the longest contiguous run of lowercase letters that advances exactly one alphabet position at each step without wrapping.

Measure the Longest Substring Advancing One Alphabet Letter at a Time

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem An alphabetical continuous substring is a contiguous piece of the ordinary alphabet abcdefghijklmnopqrstuvwxyz. In lowercase `s`, return the greatest length of a substring whose next letter is always exactly one alphabet position later. ```cpp class Solution { public: int longestContinuousSubstring(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "xyza" Output: 3 Explanation: xyz is valid, but the alphabet does not wrap from z to a. ``` **Example 2:** ```text Input: s = "ace" Output: 1 Explanation: Every neighboring pair skips a letter, so only singleton substrings qualify. ``` ### Constraints - `1 <= s.length <= 100000`. - s contains lowercase English letters. ### Clarifying Questions - Can letters be skipped? No; the result must be a substring of `s`. - Does `z` continue to `a`? No. - Can a single letter qualify? Yes. - Can equal adjacent letters belong to one valid run? No; each next letter must advance exactly one position. ```hint Check the boundary Check `"za"` and `"aa"` against the exact next-letter rule. ```

Overview: Find the longest contiguous run of lowercase letters that advances exactly one alphabet position at each step without wrapping.

IBM logo
IBM
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

An alphabetical continuous substring is a contiguous piece of the ordinary alphabet abcdefghijklmnopqrstuvwxyz. In lowercase s, return the greatest length of a substring whose next letter is always exactly one alphabet position later.

class Solution {
public:
    int longestContinuousSubstring(string s);
};

Examples

Example 1:

Input: s = "xyza"
Output: 3
Explanation: xyz is valid, but the alphabet does not wrap from z to a.

Example 2:

Input: s = "ace"
Output: 1
Explanation: Every neighboring pair skips a letter, so only singleton substrings qualify.

Constraints

  • 1 <= s.length <= 100000 .
  • s contains lowercase English letters.

Clarifying Questions Guidance

  • Can letters be skipped? No; the result must be a substring of s .
  • Does z continue to a ? No.
  • Can a single letter qualify? Yes.
  • Can equal adjacent letters belong to one valid run? No; each next letter must advance exactly one position.