Quick Overview

Find the longest nonrepeating substring with a sliding window and last-seen positions. Prove why the left boundary must never move backward and account for empty input and character representation.

Find the Longest Substring without Repeated Characters

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string, find the maximum length of a contiguous substring containing no repeated character. ### Constraints and Clarifications Use a length-only return value as the practice interface, so tied longest substrings need no tie-breaking rule. Treat the input as a sequence of characters with exact equality and no normalization; no encoding or numerical size bounds were reported. The empty string returns zero. ### Example For "abcaef", the longest valid substring is "bcaef", so the length is 5. ### What a Strong Answer Covers Maintain a valid sliding window, advance its left boundary only when necessary, and avoid moving it backward when a character was last seen before the current window. ### Follow-up Questions - Why is max(left,last_seen+1) needed in a last-position implementation? - How would you return a substring as well as its length after defining a tie rule?

Overview: Find the longest nonrepeating substring with a sliding window and last-seen positions. Prove why the left boundary must never move backward and account for empty input and character representation.

Given a string, return the maximum length of a contiguous substring containing no repeated character. Implement longestUniqueSubstringLength(text). Compare characters exactly and do not normalize them. Return only the length, so tied longest substrings need no tie rule. The empty string returns zero.

Constraints

  • text is a string of characters; the source gives no length bound or encoding restriction.
  • A substring is contiguous and each of its characters must be distinct.
  • Equality is exact and no normalization is applied.
  • Return 0 for the empty string.

Examples

Input: ('',)

Expected Output: 0

Explanation: There is no nonempty substring.

Input: ('a',)

Expected Output: 1

Explanation: The one character is distinct.

Hints

  1. A substring with no repeated character may start or end anywhere in the text.
  2. The result is a length, so equal-length substrings produce the same answer.

Loading coding console...

Show the approach

Approach

Maintain the left boundary of a window with distinct characters. For each character, if its last occurrence is inside the current window, move left to one position after that occurrence; otherwise leave left unchanged. The window remains valid because the only possible new duplicate is the incoming character, and removing through its prior occurrence eliminates it. Record the largest window length. JavaScript, Java, and C++ iterate Unicode code points for the same character-level behavior as Python. Every candidate window ending at the current position is considered at its longest valid left boundary, so the maximum found is optimal.

Time complexity:
O(n)
Space complexity:
O(n)