Given a string, return the length of its longest run: the longest stretch of consecutive positions that all hold the same character.
Function Signature
def longest_run(s: str) -> int:
Rules
-
A run is a non-empty block of consecutive, identical characters, for example
"ccc"
inside
"abccca"
.
-
Equal characters that are not adjacent never join into one run:
"abab"
consists of four runs of length 1, so its answer is
1
.
-
Return the length of the longest run as an integer. When several runs share the maximum length, that length is the answer.
Constraints
-
1 <= len(s) <= 10^5
-
s
contains only lowercase English letters
a
to
z
.
-
The answer is always between
1
and
len(s)
.
Examples
Example 1
Input: s = "aabbbcc"
Output: 3
The runs are "aa", "bbb" and "cc"; the longest is "bbb".
Example 2
Input: s = "mississippi"
Output: 2
The string contains four s characters, but they form two separate runs. The runs "ss", "ss" and "pp" all have length 2, and no run is longer.
Example 3
Input: s = "xyzzzz"
Output: 4
The longest run, "zzzz", ends at the last character of the string.