Ctrl K

Shortest Stretch With a Repeat

Medium
Parcels travel along a conveyor belt; the i-th parcel carries label labels[i]. An operator takes several **consecutive** parcels off the belt and wants at least two of them to share a label. Return the smallest number of consecutive parcels that must be taken. If no two parcels on the belt share a label, return -1.
Example 1
labels=[5, 2, 8, 2, 5, 8] → 3

The stretch [2, 8, 2] of length 3 holds two parcels labelled 2. For labels 5 and 8 the stretches are longer: 5 and 4.

Example 2
labels=[1, 2, 3] → -1

All labels differ, so the answer is -1.

Constraints
  • 1 ≤ len(labels) ≤ 10⁵
  • 0 ≤ labels[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo-Genre Stretch of a Playlist MediumWindows With an Exact Alarm Count MediumStretches With k Odd Numbers MediumTwo Separate Segments With a Given Sum MediumTrim the Ends to a Target Medium
loading Python…
Input
labels=[5, 2, 8, 2, 5, 8]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.