Given a string s, return the length of the longest substring of s in which every character differs from every other character.
A substring is a contiguous block of characters of s. The empty string counts as a substring, so an empty input has answer 0. Characters are compared exactly as given: an uppercase letter and its lowercase form are different characters, and spaces, digits and punctuation are ordinary characters that can repeat like any other.
Function Signature
def longest_distinct_substring_length(s: str) -> int:
Rules
-
Only contiguous substrings count. Picking characters that are not adjacent in
s
does not form a substring.
-
Return only the length. Several different substrings may share the maximum length; the length is still a single, unique answer.
Constraints
-
0 <= len(s) <= 50000
-
Every character of
s
is printable ASCII (character codes 32 through 126): letters, digits, symbols and the space character.
-
Because there are only 95 such characters, the answer is between 0 and
min(len(s), 95)
.
Examples
Example 1
Input: s = "streets"
Output: 4
The substring "stre" has four distinct characters. Making it one character longer would include a second "e", and no substring of five or more characters is free of repeats.
Example 2
Input: s = "aAbBa"
Output: 4
"aAbB" and "AbBa" both have four distinct characters, because "a" and "A" are different. The whole string repeats "a".
Example 3
Input: s = ""
Output: 0
The only substring of the empty string is the empty string itself.