Longest run with no repeats
Find the length of the longest stretch of consecutive characters in which no character appears twice.
longest_unique_run("abbacdeb") # 5
longest_unique_run("aaaa") # 1In the first example, bacde and acdeb both have length 5.
Sliding window
Checking every substring is far too slow. Keep a window with a left and a right edge. Move the right edge one step at a time and remember where you last saw each character.
- If the new character was last seen inside the window, move the left edge just past that spot.
- If it was last seen before the window, ignore it. The left edge must never move backward.
Try "abba" by hand. When the last a arrives, its previous position is already outside the window, and moving left back there would be wrong. The answer is 2.
Your task
Write longest_unique_run(text) returning the length of the longest substring with no repeated character.
The explanation opens here once every check passes.