FastPrepLongest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

Nykaa logoNykaa● MediumFULLTIMEPHONE SCREEN

Problem statement

Given a string s, return the length of the longest substring of s in which every character is different.

A substring is a run of consecutive characters. Characters are compared exactly: A and a are different, and spaces, digits, and symbols are characters too.

If s is empty, return 0.

Function

lengthOfLongestSubstring(s: String) → int

Examples

Example 1

s = "abcbdeb"return = 4

The substring "cbde" has 4 different characters. Every substring of length 5 contains b twice, so the answer is 4.

Example 2

s = "aaaa"return = 1

Any substring longer than one character repeats a, so the answer is 1.

Example 3

s = "Hi, hi!"return = 5

H and h are different characters, and the comma and the space count as characters too. "Hi, h" has 5 different characters. Both substrings of length 6 contain i twice, so the answer is 5.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s contains only printable ASCII characters (codes 32 to 126): English letters, digits, spaces, and symbols.

More Nykaa problems

See Nykaa hiring insights
Saved
public int lengthOfLongestSubstring(String s) {
    // Write your code here.
}
Checking account…