Solution to LeetCode Longest Substring Without Repeated Characters problem.
The idea is that last[i]
is the last occurence of the letter (with ascii code) i
, or \(-1\) if it hasn’t appeared yet. ‘cur_len’ is the length of the current sequence we are considering.
First, process the first character and setup all the variables accordingly. Then, for every other character:
- Check if it has already appeared before. If not, then no problem, increment
cur_len
and mark it as seen. - Otherwise, there are two cases:
- We check if the last occurence of char \(i\) is part of the sequence we are currently considering. If not, we are fine, increment
cur_len
and continue - Otherwise, we have a new contiguous substring without repeated characters. We update the maximum, and update
cur_len
- We check if the last occurence of char \(i\) is part of the sequence we are currently considering. If not, we are fine, increment