Sliding window · Medium
A diagnostic tool scans a sequence of one-character event codes. It highlights the longest uninterrupted run in which no code appears twice. This is a string exercise, not a parser for logs or Unicode graphemes.
Return the length of the longest contiguous substring of s with no repeated characters.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "s": string }.
Fields
- s is a string of printable ASCII characters, including spaces. Uppercase and lowercase codes are different.
Output
Return value
- Return the length, not the substring. Tied substrings require no tie-break because their lengths are equal.
- The selected characters must be adjacent in s.
- An empty string returns 0.
Constraints
- 0 <= s.length <= 100000.
- Characters have ASCII codes 32..126; Unicode normalization is outside this task.
Examples
Example 1
{
"s": "abba"
}Returns
2ab and ba are valid; abb is not.
Example 2
{
"s": "Aa bA"
}Returns
4A, a, space and b form a valid length-four run.
Required complexity
O(n) time and O(95) extra space