[LeetCode] Length of the Longest Alphabetical Continuous Substring

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

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int longestContinuousSubstring(string s) {
int res = 1;
for(int i = 1, now = 1; i < s.length(); i++) {
if(s[i] == s[i-1] + 1) now++;
else now = 1;
res = max(res, now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/18/PS/LeetCode/length-of-the-longest-alphabetical-continuous-substring/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.