Skip to content
Veltoryx Technologies
FreeProblem 28● MediumLeetCode #3 ↗4 min readUpdated 30 October 2026

Longest Substring Without Repeating Characters

Find the length of the longest contiguous substring that contains no duplicate characters using a sliding window and last-seen table.

01The problem

Given a string s, find the length of the longest substring without repeating characters. A substring is a contiguous non-empty sequence of characters within a string.

InputOutputWhy
"abcabcbb"3The answer is "abc", with length 3
"bbbbb"1The answer is "b", with length 1
"pwwkew"3The answer is "wke", with length 3 ("pwke" is a subsequence, not a substring)
""0Empty string has length 0

Constraints: 0 to 50,000 characters, consisting of English letters, digits, symbols, and spaces.

02How to spot it

  • Looking for the longest contiguous piece.
  • The validity condition is: no repeated characters in the window.
  • When a duplicate character is encountered at right, we can jump left directly past the previous occurrence of that character instead of shrinking one step at a time.

So: variable-size sliding window with a last-seen index table.

03Pseudocode

text
last[c] = 0 for every char
left = 0 ; best = 0
for right from 0 to n - 1:
  c = s[right]
  left = max(left, last[c])
  best = max(best, right - left + 1)
  last[c] = right + 1
return best
  1. Grow the window to the right by advancing right.
  2. If c was seen inside the current window, jump left past its previous location.
  3. Record the maximum window length seen, and store right + 1 as the new position for c.

04Watch it work

Visualizer · step through the algorithms = "abcabcbb"
a0
b1
c2
a3
b4
c5
b6
b7
leftright

Initialize last table for characters. Window starts at left = 0, best = 0.

Pseudocode

  1. last[c] = 0 for every char
  2. left = 0 ; best = 0
  3. for right from 0 to n - 1:
  4. c = s[right]
  5. left = max(left, last[c])
  6. best = max(best, right - left + 1)
  7. last[c] = right + 1
  8. return best

Variables

left
0
best
0
step 1 / 15

05Step-by-step dry run

  1. 1
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Initialize last table for characters. Window starts at left = 0, best = 0.

    left = 0best = 0

  2. 2
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "a" has length 1 with no duplicate characters. New best: 1.

    left = 0right = 0len = 1best = 1

  3. 3
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "ab" has length 2 with no duplicate characters. New best: 2.

    left = 0right = 1len = 2best = 2

  4. 4
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "abc" has length 3 with no duplicate characters. New best: 3.

    left = 0right = 2len = 3best = 3

  5. 5
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    'a' was previously seen inside the window (index 0). Jump left to 1 past the old copy.

    c = aleft = 1right = 3best = 3

  6. 6
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "bca" has length 3 with no duplicate characters.

    left = 1right = 3len = 3best = 3

  7. 7
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    'b' was previously seen inside the window (index 1). Jump left to 2 past the old copy.

    c = bleft = 2right = 4best = 3

  8. 8
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "cab" has length 3 with no duplicate characters.

    left = 2right = 4len = 3best = 3

  9. 9
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    'c' was previously seen inside the window (index 2). Jump left to 3 past the old copy.

    c = cleft = 3right = 5best = 3

  10. 10
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "abc" has length 3 with no duplicate characters.

    left = 3right = 5len = 3best = 3

  11. 11
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    'b' was previously seen inside the window (index 4). Jump left to 5 past the old copy.

    c = bleft = 5right = 6best = 3

  12. 12
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "cb" has length 2 with no duplicate characters.

    left = 5right = 6len = 2best = 3

  13. 13
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    'b' was previously seen inside the window (index 6). Jump left to 7 past the old copy.

    c = bleft = 7right = 7best = 3

  14. 14
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7
    leftright

    Window "b" has length 1 with no duplicate characters.

    left = 7right = 7len = 1best = 3

  15. 15
    a0
    b1
    c2
    a3
    b4
    c5
    b6
    b7

    Longest substring without repeating characters has length 3.

    best = 3

06Java solution

LongestSubstringWithoutRepeating.java
public class LongestSubstringWithoutRepeating {
    public static int lengthOfLongestSubstring(String s) {
        int[] last = new int[128];
        int left = 0, best = 0;
        for (int right = 0; right < s.length(); right++) {
            char c = s.charAt(right);
            left = Math.max(left, last[c]);
            best = Math.max(best, right - left + 1);
            last[c] = right + 1; // 1-based index: 0 means never seen
        }
        return best;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
        System.out.println(lengthOfLongestSubstring("bbbbb"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
        System.out.println(lengthOfLongestSubstring(""));
    }
}
Expected output
3
1
3
0

Tricky lines, decoded

  • Math.max(left, last[c]): Prevents left from ever moving backwards. In "abba", when the second 'a' at index 3 is reached, its last position was index 0 (last['a'] = 1), but left is already at 2 past the 'b'. Taking the maximum keeps left at 2.
  • last[c] = right + 1: Stores index + 1 so that the default value 0 cleanly represents "never seen yet".

07Why it works

For each right, the longest valid window ending at right starts immediately after the latest previous occurrence of any character within the window. Storing the most recent position of each character in last[c] lets left jump past the duplicate in O(1). Because left only moves forward, every substring ending at right is evaluated without redundant re-checks.

Invariant: Substring s[left .. right] contains no repeating characters.

08Time and space

  • Time: O(n). A single pass where right advances from 0 to n - 1 and left only jumps forward.
  • Space: O(1). The ASCII table int[128] uses fixed memory regardless of the string length n.

09Edge cases

InputOutputWhy it matters
""0Empty string
" "1A space is a valid character
"abba"2left must not move backwards
"dvdf"3Duplicate appears in the middle of a valid sequence
"tmmzuxt"5First 't' was outside the active window

10Common mistakes

  • Moving left backward. Writing left = last[c] instead of left = Math.max(left, last[c]). On "abba", the second 'a' would pull left back from 2 to 1, returning an incorrect answer of 3.
  • Clearing the set. Using a HashSet and calling set.clear() on repeat throws away valid, non-repeating characters between the old duplicate and right.
  • Using int[26]. The problem constraints include spaces, digits, and punctuation symbols. An array of size 26 will cause an ArrayIndexOutOfBoundsException. Use int[128] (or a hash map for Unicode).

11Other approaches

ApproachTimeSpaceUse when
Check all substringsO(n³)O(n)Never in an interview
Window + Set crawlO(n)O(k)Easy to explain, shrinks one by one
Last-index jumpO(n)O(1)Default interview answer
HashMap last-indexO(n)O(k)Full Unicode or arbitrary charset

12In the interview

  • Return the substring itself? Track bestStart and bestLen whenever right - left + 1 > best. Return s.substring(bestStart, bestStart + bestLen).
  • Why is jumping O(n) overall? Because left only moves forward, the sum of all jumps across the entire string is at most n.

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why do we write left = Math.max(left, last[c]) instead of left = last[c]?

  2. 2. What does storing right + 1 in the last array achieve?

14Solve it yourself

Try it on LeetCode #3.