Longest Streak of a Repeated Character in a String
StringsAuto-graded · 22 test casesLast updated
Scan a string once to find the longest streak of adjacent equal characters, keeping separated streaks distinct and using only constant extra memory.
Longest Streak of a Repeated Character
You are given a string text made of ASCII characters. A streak is a non-empty stretch of consecutive positions in the string that all hold the same character. Your task is to return the length of the longest streak.
Streaks are measured independently. If the same character shows up in several places separated by other characters, those appearances are not combined. Only characters that sit directly next to each other count toward the same streak.
Comparison is exact, so uppercase and lowercase letters are different characters. Whitespace characters (spaces, tabs) are ordinary characters and can form streaks too.
If text is empty, there are no streaks, so return 0.
Aim for linear time and constant auxiliary space.
Input
text: a string of zero or more ASCII characters.
Output
- An integer: the length of the longest streak of identical adjacent characters, or
0for an empty string.
Constraints
0 <= len(text) <= 10^6textcontains only printable ASCII characters and whitespace.- The solution should run in
O(n)time and useO(1)extra space.
Examples
Example 1
Input: longest_streak('aabbbccccdd')
Output: 4
Example 2
Input: longest_streak('ababba')
Output: 2
Example 3
Input: longest_streak('')
Output: 0
Hints
Hint 1
Think about what you need to remember while moving left to right: how long the streak ending at the current position is, and the best length seen so far.
Hint 2
Compare each character with the one just before it. If they match, extend the current streak; otherwise the streak restarts at length 1.
Hint 3
Update the best length after every step rather than only when a streak ends, so the final streak is never missed. Handle the empty string before setting any initial values.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A streak ends exactly when the character changes, so a single left-to-right pass with two counters is enough:
current: the length of the streak that ends at the current index.best: the longest streak seen so far.
Handle the empty string first and return 0. Otherwise start with both counters at 1, because the first character is a streak of length one. For each index i from 1 onward, compare text[i] to text[i - 1]:
- If they are equal, the streak continues, so increase
currentby one. - If they differ, a new streak begins, so reset
currentto1.
After each step, set best to the larger of best and current. Updating on every step means the last streak is accounted for without any extra check after the loop.
Why it is correct
After processing index i, current is the length of the maximal streak ending at i, and best is the longest streak fully inside text[0..i]. Any streak in that prefix either lies entirely in text[0..i-1] (already reflected in best) or ends at i (reflected in current), so taking the maximum keeps the invariant true. At the final index, best covers the whole string. Because current resets whenever the character changes, separated occurrences of the same character are never merged, which is why "ababba" gives 2 rather than 3.
Complexity
- Time:
O(n), since each character is examined once. - Space:
O(1), since only two integers and a loop index are used.
Reference solution
def longest_streak(text: str) -> int:
if not text:
return 0
best = 1
current = 1
for i in range(1, len(text)):
if text[i] == text[i - 1]:
current += 1
else:
current = 1
if current > best:
best = current
return best