Skip to main content
Back to problems
#2414
Medium Algorithms

Length of the longest alphabetical continuous substring

String
60.3% acceptance
Feb 25, 2026
552
38
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn longest_continuous_substring(s: String) -> i32 {
    let s = s.as_bytes();
    let mut ans = 1;
    let mut len = 1;
    for i in 1..s.len() {
      if s[i] == s[i - 1] + 1 {
        len += 1;
        ans = ans.max(len);
      } else {
        len = 1;
      }
    }
    ans
  }
}