Equal-length ties pick first or last
Updated
When two valid windows share the best length, the spec picks the first (leftmost, update only on strictly greater length) or the last (rightmost, update on >=).
What to focus on
Say the tie-break before coding. Storing bounds at the first record and refusing equals keeps the first. Writing bounds on every equal length keeps the last.
Worked examples
first unique 3
s = "abca", unique length 3, first wins- [0,2] "abc" record
- [1,3] "bca" also 3, skip equals
Result: keep L=0 R=2
last unique 3
s = "abca", unique length 3, last wins- [0,2] record
- [1,3] length==best, overwrite
Result: keep L=1 R=3
sum 10 length 2
a = [4, 1, 9, 1], sum==10 windows of length 2- [1,2] values 1,9
- [2,3] values 9,1
- first keeps [1,2], last keeps [2,3]
Result: pick a rule and stick to it
Try it
Write bestBounds(s, tie). s is a string, or a is an array of integers. tie is first or last. L and R bound a valid window that includes s[L..R]. First updates only on strictly greater length. Last updates on >=. Return the bounds for that tie rule. Longest unique on "abca" both ways. Then sum==10 length 2 on [4, 1, 9, 1].
a (or s) is nonempty. At least two valid windows share the best length.