Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
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".
"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 <= 105s consists of only English lowercase letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abacaba"
"abcde"
longest-consecutive-sequence)arithmetic-slices)max-consecutive-ones)maximum-number-of-vowels-in-a-substring-of-given-length)number-of-zero-filled-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2414: Length of the Longest Alphabetical Continuous Substring
class Solution {
public int longestContinuousSubstring(String s) {
int ans = 1, cnt = 1;
for (int i = 1; i < s.length(); ++i) {
if (s.charAt(i) - s.charAt(i - 1) == 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2414: Length of the Longest Alphabetical Continuous Substring
func longestContinuousSubstring(s string) int {
ans, cnt := 1, 1
for i := range s[1:] {
if s[i+1]-s[i] == 1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
return ans
}
# Accepted solution for LeetCode #2414: Length of the Longest Alphabetical Continuous Substring
class Solution:
def longestContinuousSubstring(self, s: str) -> int:
ans = cnt = 1
for x, y in pairwise(map(ord, s)):
if y - x == 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 1
return ans
// Accepted solution for LeetCode #2414: Length of the Longest Alphabetical Continuous Substring
impl Solution {
pub fn longest_continuous_substring(s: String) -> i32 {
let mut ans = 1;
let mut cnt = 1;
let s = s.as_bytes();
for i in 1..s.len() {
if s[i] - s[i - 1] == 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 1;
}
}
ans
}
}
// Accepted solution for LeetCode #2414: Length of the Longest Alphabetical Continuous Substring
function longestContinuousSubstring(s: string): number {
let [ans, cnt] = [1, 1];
for (let i = 1; i < s.length; ++i) {
if (s.charCodeAt(i) - s.charCodeAt(i - 1) === 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.