Skip to content
← Back to the archive

1 September 2026 · Advanced track

Longest Limited Run

Advanced Strings

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 letters
  • codes may be empty
  • max_distinct is a non-negative integer
  • If max_distinct is 0, or codes is empty, return an empty string
  • When multiple longest stretches share the same length, return the one that starts first

Function signature

def longest_limited_run(codes, max_distinct):

Sign in to solve this bracket

Reading the problem is free for everyone. Opening the editor and solving it requires an account with Open Bracket+.