Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the longest substring without repeating characters after one deletion

Given a string, optionally delete at most one character and find the length of the longest substring without repeating characters.

Python practice28 minStrings – Sliding Window & FrequencyAdvancedLast updated March 25, 2026

Problem statement

You are given a string s. You may delete at most one character from s (deletion is optional). After performing at most one deletion, determine the length of the longest substring that contains no repeating characters. Return that length. Note: deletion is optional — deleting zero characters is allowed when it yields a longer unique substring. Example: s = 'abca'. If you delete the final 'a', the string becomes 'abc' and the longest non-repeating substring length is 3.

Task

Explore strategies (brute-force removal + sliding-window) to determine, after optionally deleting one character, the maximum length of a substring with all unique characters.

Examples

Delete to maximize unique substring

Input

longest_substring_after_one_deletion("abca")

Output

3

Delete the final 'a' to get 'abc' with longest unique substring length 3.

Input format

Call longest_substring_after_one_deletion(s) with a string s.

Output format

Return an integer equal to the maximum length of a substring with all distinct characters after at most one deletion.

Constraints

0 <= len(s) <= 2000. s may include any ASCII characters. An O(n^2) algorithm is acceptable for this lesson.

Samples

Sample 1

Input

longest_substring_after_one_deletion("eceba")

Output

4

Deleting the second 'e' yields a string where 'ecba' (or 'ceba') is length 4 with unique characters.