Longest Substring Without Repeat

Medium

Given a string s, find the length of the longest substring without repeating characters.

Example
s: “workattech”
Result: 6
Explanation: Longest vaild substring is “workat”.
s: “mississippi”
Result: 3
Explanation: Longest vaild substrings are “mis” and “sip”, both of length 3.

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of test cases.

For each test case, the input has two lines:

  • An integer ‘n’ denoting the length of the string.
  • The string s.

Output Format

For each test case, the output contains a line with an integer denoting the length of the longest substring in s having distinct characters.

Sample Input

2
10
workattech
11
mississippi

Expected Output

6
3

Constraints

1 <= T <= 10
1 <= n <= 106
The string s consists of lowercase English characters.

Editorial Link: Editorial