2414. Length of the Longest Alphabetical Continuous Substring

Difficulty:
Related Topics:
    Similar Questions:

    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".

    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:

    Solution (Java)

    class Solution {
        public int longestContinuousSubstring(String s) {
            int x = 0, len = 0, res = 0;
            for(char ch : s.toCharArray()) {
                len = ch-'a'-x==1 ? len+1 : 1;  // add to len or restart
                res = Math.max(res, len);
                x = ch-'a';
            }
            return res;
        }
    }
    

    Explain:

    nope.

    Complexity: