#K67137. Longest Unique Substring

    ID: 32576 Type: Default 1000ms 256MiB

Longest Unique Substring

Longest Unique Substring

Given a string s, your task is to determine the length of the longest contiguous substring that contains no repeating characters. In other words, find the maximum length L such that there exists a segment s[i...j] where all characters are unique. Mathematically, the length is defined as:

$L = \max_{0 \leq i \leq j < n} (j - i + 1)$,

subject to the condition that each character in s[i...j] is distinct. This problem can be efficiently solved using a sliding window or two-pointer approach, combined with a hash table to track the last seen index of characters.

For example:

  • For s = "abcabcbb", the answer is 3 because "abc" is the longest substring without repeating characters.
  • For s = "bbbbb", the longest substring is "b" with length 1.
  • For s = "pwwkew", one possible answer is 3 (substring "wke").

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  1. The first line contains an integer T, the number of test cases.
  2. The following T lines each contain a non-negative string s.

Constraints:

  • 1 ≤ T ≤ 104
  • 0 ≤ |s| ≤ 105 (|s| is the length of string s)

outputFormat

For each test case, output a single integer on a new line representing the length of the longest contiguous substring of unique characters.

Output is written to standard output (stdout).

## sample
3
abcabcbb
bbbbb
pwwkew
3

1 3

</p>