#C11089. Longest Unique Substring

    ID: 40366 Type: Default 1000ms 256MiB

Longest Unique Substring

Longest Unique Substring

Given a string S, your task is to find the longest substring that contains no repeating characters. In case there are multiple substrings with the same maximum length, output the one that appears first in S.

Note: The input will be read from standard input (stdin) and the output should be written to standard output (stdout).

For example:

  • If S = "abcabcbb", the answer is abc.
  • If S = "bbbbb", the answer is b.
  • If S = "pwwkew", the answer is wke.

The solution involves a sliding window technique and can be mathematically analyzed as follows: Let the string be indexed from 0 to n-1. For each index i, maintain the last occurrence of each character. When a duplicate is found at index i that lies within the current window, shift the window's starting index to last_occurrence(char) + 1. The length of the current substring is i - start + 1. The goal is to maximize this length.

In mathematical terms, if we denote:

\text{current_length} = i - \text{start} + 1

then we wish to maximize current_length subject to the condition that all characters in the substring are unique.

inputFormat

The input consists of a single line containing a string S. The string can contain letters, digits, spaces, and special characters.

outputFormat

Output the longest substring of S that does not contain any repeating characters. If there are multiple answers, output the one that appears first.## sample

abcabcbb
abc