Unique-character window shrinks on a repeat09.04
When s[R] already has count 1 in the window, the new count is 2. Shrink L until that character's count is 1, usually L moves to lastIndex[s[R]]+1.
The repeat is the trigger. Do not shrink on a fresh character. After the shrink, every count is 1 again and R can stay.
Same skill, three traces. Read all three before you try it.
01second a - given
s = "abca",R=3- walk
window was
"abc",cnt[a]=1s[3]='a'→2drop
s[0]='a'→1,L=1- result
"bca"unique
02abcabcbb - given
s = "abcabcbb",R=3- walk
s[3]='a'repeatss[0]Lmoves to1window
"bca"- result
- shrink on the repeat, not before
03late bb - given
s = "abcabcbb",R=7- walk
s[7]='b'repeats thebalready in the windowshrink until one
bremains- result
- window
"b"at the end
uniqueShrinkOnRepeat(s).s is a string. R is the current right index that repeats a character still inside [L, R]. L is the left index. The window includes s[L..R].At each
R print whether you shrunk and the new L. Return nothing.On
"abca" and "abcabcbb".constraintss is nonempty. The current R repeats a character still inside [L, R].
- 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.