Last Seen Jump Left

When To Use

Use this when duplicates can be handled by jumping l directly instead of removing one element at a time.

Template

l = 0
last = map()
 
for r = 0; r < n; r++:
    if s[r] in last:
        l = max(l, last[s[r]] + 1)
 
    last[s[r]] = r
    update answer

Problems Using This Pattern

Longest Substring Without Repeating Characters

Problem: find the longest substring where every char appears at most once.

State: last index where each char appeared.

Maximum Erasure Value

Problem: find the max-sum subarray with all unique values.

State: jump l past the duplicate and maintain the current sum.

Longest Nice Substring Variant

Problem: find a longest substring after excluding invalid chars or duplicate state.

State: when a char makes the current window impossible, jump the left boundary.