DSA and coding Level 2 This level · 20 of 30
Medium dsa sliding-window

Longest Substring Without Repeating Characters

The 30 second answer

I grow a window over the string and store the last index of each character. When the character at the right edge was already inside the window, I move the left edge to one past that earlier index. The answer is the longest window I see. Linear time and space proportional to the alphabet.

Could you say this in an interview?

Problem Given a string, return the length of the longest substring that contains no repeated character. Original: LeetCode 3: Longest Substring Without Repeating Characters.

Understanding the prompt It is a substring, so the characters must be contiguous. The result is a length, not the substring itself. An empty string has length 0. Repeats that fall to the left of the current window do not count.

Pattern recognition A variable-size window whose constraint is 'all characters unique'. The condition is monotonic: growing the window can only introduce a repeat, and shrinking from the left can only remove one.

Brute force Check every substring and test uniqueness with a set: O(n²) or worse.

Optimal A map from character to its last seen index, and a left edge lo. For each hi, if that character was already seen at an index >= lo, move lo to that index + 1. Then store hi as the new last index and update the best length hi - lo + 1.

Alternatives A set plus a while loop that shrinks from the left until the new character is free is the same idea, often a little easier to write, still O(n).

Java solution

import java.util.HashMap;

public class LongestSubstringWithoutRepeatingCharacters {
    static int lengthOfLongestSubstring(String s) {
        var last = new HashMap<Character, Integer>();
        int lo = 0, best = 0;
        for (int hi = 0; hi < s.length(); hi++) {
            var prev = last.put(s.charAt(hi), hi);
            if (prev != null && prev >= lo) lo = prev + 1;
            best = Math.max(best, hi - lo + 1);
        }
        return best;
    }

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

Complexity O(n) time, O(min(n, alphabet)) extra space.

Edge cases

  • All characters the same, as in "bbbbb", returns 1.
  • Repeats that restart the window, as in "pwwkew", whose longest unique substring is "wke" with length 3, not "pwke".
  • The empty string returns 0.

Follow-ups

  • Longest substring with at most k distinct characters, or Fruit Into Baskets (at most two distinct).
  • This problem is regularly named in public Microsoft interview reports.

Tests "abcabcbb" returns 3. "bbbbb" returns 1. "pwwkew" returns 3. "" returns 0.

How to say it in the interview "I grow a window and remember the last index of each character. When the right character is already inside the window, I jump the left edge to one past that earlier index. The longest window I see is the answer. Linear time."

Sources (3)
  1. LeetCode 3: Longest Substring Without Repeating Charactersleetcode.com
  2. Java SE 17 API: HashMapdocs.oracle.com
  3. LeetCode Discuss: Microsoft SDE-2 recent questions 2025 (candidate reports, secondary)leetcode.com
Previous
Next