All problems
MediumStrings

Longest Substring Without Repeating Characters

amazongooglemicrosoftmetaadobe

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

A substring is a contiguous non-empty sequence of characters within a string.

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.

Examples

Example 1

Input: s = "abcabcbb"

Output: 3

Explanation: The longest substring without repeating characters is "abc" (indices 0-2), length 3.

Example 2

Input: s = "pwwkew"

Output: 3

Explanation: The longest substring without repeating characters is "wke" (indices 2-4) or "kew" (indices 3-5), length 3.

Constraints

  • -0 <= s.length <= 5 * 10^4
  • -s consists of English letters, digits, symbols and spaces.

Optimal Complexity

Time

O(n)

Space

O(min(n, m))

Practice this problem with an AI interviewer

TechInView conducts a full voice mock interview — the AI asks clarifying questions, evaluates your approach, watches you code, and scores you on 5 dimensions. Just like a real FAANG interview.

Start a free interview