Problem · String

Longest Substring Without Repeating Characters

Learn this problem
MediumNetflix logoNetflixFULLTIMEPHONE SCREEN

Problem statement

Given a string s, return the length of the longest contiguous substring whose characters are all distinct.

For this exercise, assume s contains printable ASCII characters. The empty string has answer 0.

Function

lengthOfLongestSubstring(s: String) → int

Examples

Example 1

s = "abcabcbb"return = 3

The substring abc has three distinct characters. Any longer substring repeats at least one character.

Example 2

s = "pwwkew"return = 3

Both wke and kew have length 3. The answer concerns a contiguous substring, not a subsequence.

Example 3

s = ""return = 0

The empty string contains no non-empty substring, so its longest valid length is 0.

Constraints

  • 0 <= s.length <= 100000
  • s contains printable ASCII characters.

More Netflix problems

drafts saved locally
public int lengthOfLongestSubstring(String s) {
  // write your code here
}
s"abcabcbb"
expected3
checking account