--- ###### tags: `Leetcode` --- # Leetcode 2414. Length of the Longest Alphabetical Continuous Substring [link](https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/) --- 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. #### 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 <= 105 - s consists of only English lowercase letters. --- Using two pointers to iterate each slice of the given string, if the slice is consecutive, store the length of that slice and return the maximum. #### Solution 1 ```python= class Solution: def longestContinuousSubstring(self, s: str) -> int: alphabet = "abcdefghijklmnopqrstuvwxyz" ans = 1 l = 0 r = l + 1 while l < len(s) - 1: while s[l:r+1] in alphabet and r < len(s): count = r - l + 1 ans = max(count, ans) r += 1 l = r r = l + 1 return ans ``` O(T): O(N) O(S): O(1) --- We can iterate the given string from length 1 to len(s). By checking whether the slice is consecutive, store the maximum length of that consecutive substring. #### Solution 2 ```python= class Solution: def longestContinuousSubstring(self, s: str) -> int: start = maxLen = 0 for i in range(1, len(s) + 1): if s[start:i] in alphabet: maxLen = max(maxLen, i - start) else: start = i - 1 return maxLen ``` O(T): O(N) O(S): O(1)