Loading...
Loading...
You are given a string s of lowercase English letters.
A substring of s is a contiguous, non-empty sequence of characters inside s. Call a substring good if its first character comes strictly before its last character in alphabetical order. A substring of length 1 is never good, because its first and last characters are the same.
Return the length of the longest good substring of s. If s has no good substring, return 0.
Input: s = "dbabcb"
Output: 4
Explanation: "babc" (indices 1 to 4) starts with 'b' and ends with 'c', and "abcb" (indices 2 to 5)
starts with 'a' and ends with 'b'. Both are good and have length 4. The two substrings of length 5
are "dbabc" ('d' is after 'c') and "babcb" ('b' equals 'b'), and the whole string starts with 'd'
and ends with 'b', so nothing longer is good.
Input: s = "az"
Output: 2
Explanation: The whole string starts with 'a' and ends with 'z', and 'a' comes before 'z'.
Input: s = "cba"
Output: 0
Explanation: Every substring of length 2 or more starts with a letter that comes after its last
letter ("cb", "ba", "cba"), so no substring is good.
s.length ≤105s consists of lowercase English letters only.