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.
Input: s = "abacaba" Output: 2 Explanation: There are 4 distinct continuous substrings: "a", "b", "c" and "ab". "ab" is the longest continuous substring.
Input: s = "abcde" Output: 5 Explanation: "abcde" is the longest continuous substring.
1 <= s.length <= 105
s
consists of only English lowercase letters.
impl Solution {
pub fn longest_continuous_substring(s: String) -> i32 {
let s = s.as_bytes();
let mut count = 1;
let mut ret = 1;
for i in 1..s.len() {
if s[i - 1] + 1 == s[i] {
count += 1;
ret = ret.max(count);
} else {
count = 1;
}
}
ret
}
}