Longest Substring Without Repeating Characters
- Difficulty: Medium
- Source: LeetCode 3 – Longest Substring Without Repeating Characters
Description
You are given a string s, and the goal is to determine the maximum length of any substring that has all unique characters, meaning no character appears more than once in that substring.
The substring must be contiguous within s (no reordering or skipping), and you only need to return the length of the longest such substring, not the substring itself.
Example
Example 1:
- Input:
s = "abcabcbb" - Output:
3 - Explanation: One longest substring without repeating characters is
"abc", which has length 3.
Example 2:
- Input:
s = "bbbbb" - Output:
1 - Explanation: Every substring with unique characters is just
"b", so the maximum length is 1.
Example 3:
- Input:
s = "pwwkew" - Output:
3 - Explanation: A valid longest substring is
"wke"with length 3; note that"pwke"is not allowed because it is not contiguous.
You can test edge cases like s = "" (empty string) or s = " " (single space) to see how the result behaves.[6][8]
Code
# LeetCode 3: Longest Substring Without Repeating Characters
# Credit: Problem from LeetCode (see problem page for full statement and tests).
def lengthOfLongestSubstring(s: str) -> int:
"""
Write your solution here.
Requirements:
- Consider contiguous substrings of s.
- Within the chosen substring, all characters must be distinct.
- Return the maximum length among all such substrings.
"""
# To be solved
raise NotImplementedError