FastPrepLongest Distinct String

Longest Distinct String

Superhuman logoSuperhuman● MediumNEW GRADINTERNOA
Learn

Problem statement

Given a string s, return the length of its longest contiguous substring in which no character appears more than once.

A substring is a contiguous sequence of characters within s. Return only the maximum length, not the substring itself.

Function

longestDistinctString(s: String) → int

Examples

Example 1

s = "abcc"return = 3

The substring "abc" contains no repeated character and has length 3.

Example 2

s = "abcabcbb"return = 3

Substrings such as "abc" have length 3, and every longer substring repeats a character.

Example 3

s = "bbbbb"return = 1

Every distinct-character substring contains only one b.

Constraints

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

Source note: The source slide shows the objective, uniqueness rules, and the worked abcc example.

More Superhuman problems

See Superhuman hiring insights
public int longestDistinctString(String s) {
  // Write your code here.
}
s"abcc"
expected3
Checking account…