# LeetCode 3 – Longest Substring Without Repeated Characters

#### Posted on December 21, 2013

##### Last updated on December 21, 2013

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
Markdown SHA1: 8eb6e4dc13ae384b09d2f670db27d4a8b4e0bb69