Longest Substring Without Repeating Characters
Company: Antra
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Longest Substring Without Repeating Characters
Given a string `s`, find the length of the **longest substring** that contains no repeating characters.
A substring is a contiguous, non-empty sequence of characters within the string. The string may contain any printable ASCII characters, including letters, digits, symbols, and spaces.
### Examples
Example 1:
- Input: `s = "abcabcbb"`
- Output: `3`
- Explanation: The longest substring without repeating characters is `"abc"`, which has length 3.
Example 2:
- Input: `s = "bbbbb"`
- Output: `1`
- Explanation: The longest substring without repeating characters is `"b"`, with length 1.
Example 3:
- Input: `s = "pwwkew"`
- Output: `3`
- Explanation: The longest substring without repeating characters is `"wke"`, with length 3. Note that `"pwke"` is a subsequence, not a substring.
Example 4:
- Input: `s = ""`
- Output: `0`
- Explanation: The empty string has no substring, so the answer is 0.
### Constraints
- `0 <= s.length <= 5 * 10^4`
- `s` consists of printable ASCII characters (`' '` through `'~'`, i.e. character codes 32 through 126).
### Required Function
Implement a function that takes the string `s` and returns an integer: the length of the longest substring of `s` with all distinct characters.
Quick Answer: This question tests a software engineer's ability to apply sliding window techniques and string manipulation to optimize character uniqueness tracking. It is a classic coding problem that evaluates practical algorithm design, commonly used to assess proficiency with hash-based data structures and efficient string traversal in technical interviews.
Given a string `s`, find the length of the **longest substring** that contains no repeating characters.
A substring is a contiguous, non-empty sequence of characters within the string. The string may contain any printable ASCII characters, including letters, digits, symbols, and spaces.
### Examples
Example 1:
- Input: `s = "abcabcbb"`
- Output: `3`
- Explanation: The longest substring without repeating characters is `"abc"`, which has length 3.
Example 2:
- Input: `s = "bbbbb"`
- Output: `1`
- Explanation: The longest substring without repeating characters is `"b"`, with length 1.
Example 3:
- Input: `s = "pwwkew"`
- Output: `3`
- Explanation: The longest substring without repeating characters is `"wke"`, with length 3. Note that `"pwke"` is a subsequence, not a substring.
Example 4:
- Input: `s = ""`
- Output: `0`
- Explanation: The empty string has no substring, so the answer is 0.
### Constraints
- `0 <= s.length <= 5 * 10^4`
- `s` consists of printable ASCII characters (`' '` through `'~'`, i.e. character codes 32 through 126).
### Required Function
Implement a function that takes the string `s` and returns an integer: the length of the longest substring of `s` with all distinct characters.
Constraints
- 0 <= s.length <= 5 * 10^4
- s consists of printable ASCII characters (codes 32 through 126)
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The longest distinct substring is "abc" (length 3).
Input: ("bbbbb",)
Expected Output: 1
Explanation: Every character is the same, so the best window is a single "b".
Input: ("pwwkew",)
Expected Output: 3
Explanation: "wke" is the longest substring with no repeats (length 3).
Input: ("",)
Expected Output: 0
Explanation: An empty string has no substring, so the answer is 0.
Input: ("a",)
Expected Output: 1
Explanation: A single character is trivially distinct (length 1).
Input: ("abcdef",)
Expected Output: 6
Explanation: All characters are distinct, so the whole string qualifies (length 6).
Input: ("dvdf",)
Expected Output: 3
Explanation: The repeat 'd' is not at the window's left edge; the best window is "vdf" (length 3).
Input: ("tmmzuxt",)
Expected Output: 5
Explanation: After skipping past the duplicate 'm', the window "mzuxt" gives length 5.
Input: (" ",)
Expected Output: 1
Explanation: A single space is a valid one-character substring (length 1).
Input: ("abba",)
Expected Output: 2
Explanation: When the second 'a' appears, the window start must jump forward; the best is "ab" or "ba" (length 2).
Input: ("a!a!b@b@c#",)
Expected Output: 4
Explanation: Mixed letters and symbols; the longest distinct window is "b@c#" (length 4).
Hints
- Use a sliding window with two pointers. Expand the right edge one character at a time and shrink the left edge whenever a character repeats.
- Track the most recent index at which each character was seen. When you encounter a repeat that lies inside the current window, jump the window start to just past that previous occurrence.
- The answer is the maximum window width (right - left + 1) observed while scanning. This runs in O(n) time because each character is visited once.