Track the best valid length05.04
Keep bestLen. When the window is valid, bestLen = max(bestLen, R-L+1). Init bestLen to 0 if empty is allowed.
Window constraint: longest window with all unique values. Update bestLen only on unique windows. Length is R-L+1, not R-L. Ties leave bestLen unchanged.
Same skill, three traces. Read all three before you try it.
01best 3 - given
s = "abca"- walk
abc length
3→best=3bca length
3→ best stays3- result
bestLen = 3
02best 3 then 2 - given
a = [4, 1, 9, 1]- walk
[4,1,9]unique length3→best=3after shrink
[9,1]unique length22<3so best stays3- result
bestLen = 3
03best 1 - given
a = [7]- walk
[7]unique length1init
0thenmax(0,1)- result
bestLen = 1
bestUniqueLen(a).a is an array of integers, or s is a string. L is the left index. R is the right index. The window includes a[L..R]. Valid means all unique.Return the final bestLen. Ties leave bestLen unchanged.
On
s="abca"; a=[4, 1, 9, 1]; a=[7].constraintsa (or s) is nonempty.
- Longest Substring Without Repeating Characters0/3 skills ready · sliding window · mediumopen
- Longest Substring Without Repeating Characters0/3 skills ready · sliding window · mediumopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.