#C13088. Longest Unique Substring
Longest Unique Substring
Longest Unique Substring
Given a string s, your task is to find the longest substring that contains no duplicate characters. Use a sliding window approach to ensure an efficient solution. Formally, for a string \( s \), find a substring \( s[i \ldots j] \) such that all characters are unique and \( j-i+1 \) is maximized. If there are multiple answers, return the first occurrence.
For example:
- For
s = "abcabcbb"
, the longest substring is"abc"
with length 3. - For
s = "pwwkew"
, the longest substring is"wke"
.
The problem can be formally defined using the following equation:
\[ \text{maximize} \quad L = j - i + 1 \quad \text{subject to } \forall k, l \in [i,j],\; s[k] \neq s[l] \text{ for } k \neq l. \]
inputFormat
The input consists of a single line containing the string s. The string s may include any printable characters.
outputFormat
Output the longest substring of s that does not contain any repeating characters. If multiple answers exist, output the one that appears first.
## sampleabcabcbb
abc