Talentd
FleetCode

3. Longest Substring Without Repeating Characters

Medium39.9% acceptance104 companies

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

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

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

Hints open one at a time

Hint 1
Generate all possible substrings & check for each substring if it's valid and keep updating maxLen accordingly.
6:46
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def lengthOfLongestSubstring(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"abcabcbb"
Input
"bbbbb"
Input
"pwwkew"
Your code is saved on this device.Solution