Stretches With K Different Values
Return how many stretches of nums (one or more items next to each other) hold exactly k different values. Stretches at different positions count separately.
Example 1
nums [1,2,1,3]
k 2
returns 4
The stretches are [1, 2], [1, 2, 1], [2, 1] and [1, 3]. The others hold one value or three.
Example 2
nums [4,4,4]
k 1
returns 6
All six stretches hold only the value 4.
Example 3
nums [1,2,3]
k 3
returns 1
Only the whole list holds three different values.
Constraints
1 <= len(nums) <= 3001 <= k <= len(nums)-109 <= nums[i] <= 109
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.