FastPrepLongest Substring Without Repeating Characters
Problem · String

Longest Substring Without Repeating Characters

Learn this problem
MediumWex logoWexFULLTIMEPHONE SCREEN

Problem statement

You are given a string s consisting of printable ASCII characters.

Return the length of the longest substring of s that contains no repeated character.

The empty string has length 0.

Function

lengthOfLongestSubstring(s: String) → int

Examples

Example 1

s = "abcabcbb"return = 3

The substring abc has length 3 and no repeated character. Longer windows such as abca repeat a.

Example 2

s = "bbbbb"return = 1

Every character is b, so the longest non-repeating substring has length 1.

Example 3

s = "pwwkew"return = 3

The substring wke has length 3. The answer must be a contiguous substring, so pwke is not valid.

Constraints

  • 0 <= s.length <= 5 * 10^4.
  • s contains only printable ASCII characters.

More Wex problems

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