1 September 2026 · Advanced track
Longest Limited Run
Find the longest stretch of festival gate entries that uses no more than a set number of different wristband colours.
A festival's entry gate logs each visitor's wristband colour as they pass through, one letter per visitor, in the order they arrived. Find the longest unbroken stretch of the log that contains at most a given number of different wristband colours. If more than one stretch shares the longest length, return the one that starts earliest in the log. Return the matching stretch as the string itself, not its length. If the limit is 0, or the log is empty, return an empty string.
Constraints
codes is a string of uppercase letterscodes may be emptymax_distinct is a non-negative integerIf max_distinct is 0, or codes is empty, return an empty stringWhen multiple longest stretches share the same length, return the one that starts first
Function signature
def longest_limited_run(codes, max_distinct):