Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

Sample Input:   "abcabcbb"

Expected Output: 3

The answer is "abc", with a length of 3.

Constraints

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

How We Solve This

  • Use two pointers, start and end, to track the current substring.
  • Use a HashMap to remember the last index of each character.
  • Move the end pointer forward one character at a time.
  • If the character is already in the current substring, move the start pointer right after its last occurrence.
  • Update the maximum length after processing each character.

Edge Cases to Consider

Empty String

Input: ""
Output: 0

The algorithm correctly returns 0 for empty inputs.

All Same Characters

Input: "aaaa"
Output: 1

The window shrinks at every step, keeping max length at 1.

Spaces & Symbols

Input: "a b!a"
Output: 4

Spaces and symbols are treated as unique characters.

Repeats at Start/End

Input: "abca"
Output: 3

The sliding window correctly adjusts to find 'bca'.

Single Character

Input: "z"
Output: 1

The minimum valid substring is handled correctly.