Skip to main content

Length of the Longest Alphabetical Continuous Substring

Problem Description​

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.

Examples​

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​

  • 1 <= s.length <= 10^5

Solution for Frequency of the Longest Alphabetical Continuous Substring​

Approach​

  1. Initialize Variables:

    • len: Current length of the contiguous substring.
    • maxLen: Maximum length of the contiguous substring found so far.
    • prev: ASCII value of the previous character.
    • i: Current index in the string.
  2. Edge Case:

    • If the string length is 1, return 1 because the longest contiguous substring is the string itself.
  3. Iterate Through the String:

    • Use a while loop to iterate through the string.
    • If prev is -1 (indicating the start), set prev to the ASCII value of the current character and increment i.
    • If the ASCII value of the current character minus prev equals 1, it means the characters are in consecutive order.
      • Continue this check until the characters are no longer in consecutive order.
      • Update maxLen with the length of the contiguous substring found.
      • Reset prev to -1 to start checking the next possible contiguous substring.
    • If the characters are not in consecutive order, update prev to the ASCII value of the current character and increment i.
  4. Return Result:

    • After iterating through the string, return maxLen.

Implementation​

Live Editor
function Solution(arr) {
    var longestContinuousSubstring = function(s) {
    let len = 1;
    let maxLen = 1;
    let prev = -1;
    let i = 0;

    if (s.length === 1) return 1;

    while (i < s.length) {
        if (prev === -1) {
            prev = s.charCodeAt(i);
            i++;
        } else if (s.charCodeAt(i) - prev === 1) {
            let k = i;
            while (i < s.length && s.charCodeAt(i) - prev === 1) {
                prev = s.charCodeAt(i);
                i++;
            }
            maxLen = Math.max(maxLen, i - k + 1);
            prev = -1;
        } else {
            prev = s.charCodeAt(i);
            i++;
        }
    }

    return maxLen;
};

  const input = "abacaba"

  const output = longestContinuousSubstring(input)
  return (
    <div>
      <p>
        <b>Input: </b>
        {JSON.stringify(input)}
      </p>
      <p>
        <b>Output:</b> {output.toString()}
      </p>
    </div>
  );
}
Result
Loading...

Complexity Analysis​

  • Time Complexity: O(n)O(n)
  • Space Complexity: O(1)O(1)

Code in Different Languages​

Written by @hiteshgahanolia
var longestContinuousSubstring = function(s) {
let len = 1;
let maxLen = 1;
let prev = -1;
let i = 0;

if (s.length === 1) return 1;

while (i < s.length) {
if (prev === -1) {
prev = s.charCodeAt(i);
i++;
} else if (s.charCodeAt(i) - prev === 1) {
let k = i;
while (i < s.length && s.charCodeAt(i) - prev === 1) {
prev = s.charCodeAt(i);
i++;
}
maxLen = Math.max(maxLen, i - k + 1);
prev = -1;
} else {
prev = s.charCodeAt(i);
i++;
}
}

return maxLen;
};

References​