Longest Equal Run
Find the length of the longest uninterrupted run of equal lowercase letters.
EXECUTION LAB
Find the length of the longest uninterrupted run of equal lowercase letters.
Input and constraints
- Use lowercase letters encoded as 0 (a) through 25 (z).
- Use at most 32 integers. Each numeric input is between -1000 and 1000.
Code inspector
def solve(values, target): run = 0 best = 0 i = 0 while (i < len(values)): if ((i > 0) and (values[i] == values[(i - 1)])): run = (run + 1) else: run = 1 best = max(run, best) i = (i + 1) return bestVisualizer
Step 1 of 701i
11
22
32
42
51
Inspect the current state
Check whether the character continues the current run of 0.
Variable watch
i 0run 0best 0
Step explanation
Check whether the character continues the current run of 0.
comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(n) time · O(1) space
Worked explanation
A winning streak ends as soon as the jersey colour changes.
Guided solution
Only an equal neighbour extends the current run; any change starts length one.
O(n) time · O(1) auxiliary space
Examples
Worked example · {"values":[1,1,2,2,2,1],"target":0} ⇒ 3
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[4],"target":0} ⇒ 1
Progressive hints
Hint 1
A winning streak ends as soon as the jersey colour changes.Hint 2
Only an equal neighbour extends the current run; any change starts length one.Hint 3
Step through the worked example and inspect the highlighted variables before each update.Common mistake
Counting all copies combines runs that a different character separates.
Interview follow-up
How would you return the letter and first position of a longest run?