Problem Description
Given a string s
consisting of lowercase letters only, return the length of the longest alphabetical continuous substring.
Key Insights
- An alphabetical continuous substring consists of consecutive letters in the alphabet.
- We can iterate through the string while comparing each character with the previous one.
- If the current character is the next consecutive letter after the previous character, we extend the current substring length.
- If not, we reset the current substring length.
- The maximum length encountered during the iteration will be our result.
Space and Time Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Solution
To solve this problem, we will use a simple linear scan through the string. We will maintain two variables: one for the current length of the alphabetical substring and another for the maximum length found. As we iterate through the string, we will check if the current character is the consecutive letter of the previous character. If it is, we increment the current length; otherwise, we reset it to 1. Finally, we will compare the current length with the maximum length found so far and update it accordingly.