← 返回 cisco 的题目列表Longest Substring Without Repeating Characters (LC 3)
类型:qbank
Solve LeetCode 3, Longest Substring Without Repeating Characters, but return the actual substring rather than only its length.
Requirements
Given a string, identify its longest substring without repeated characters.
Return the substring itself rather than only its length.
Notes
The return-value change is the key Cisco-specific requirement; confirm how ties should be handled before coding.
Use a sliding window and map each character to its most recent index. Keep a left boundary such that the current window contains no duplicate; on a repeated character whose previous index is still inside the window, move the boundary to one position after that index.
Track the best start position and length while expanding the right boundary, then slice the string once at the end. Update on strictly longer windows by default, or apply the agreed tie rule consistently.
This runs in O(n) time and O(min(n, alphabet size)) auxiliary space.
Be prepared to explain the implementation line by line. State the window invariant before coding so an unconventional control flow remains easy to verify.
Preparation
Implement the last-seen-index version from scratch in 15 minutes, returning start/end indices internally and slicing only once after the scan.
Test the implementation on an empty string, one character, all-identical characters, a repeat inside the active window, and a repeat that lies before the current left boundary.
Trace the left boundary, current window, and best window on paper, then explain in two minutes why each index advances monotonically and the scan is O(n).