532. Find Longest Self-Contained Substring
You are given a string s. Your goal is to determine the length of the longest substring of s that is self-contained.
A substring t of s is defined as self-contained if t is not equal to s (i.e. it is a proper substring) and for every character in t, that character does not appear anywhere in s outside of t.
Return the length of the longest self-contained substring if one exists; otherwise, return -1.
Example 1:
Input : s = "aabbc"
Output : 4
Explanation :
The substring "aabb" (the first four characters) is self-contained because it includes the letters 'a' and 'b', and neither 'a' nor 'b' appears in the remaining part of the string ("c"). Hence, the answer is 4.
Example 2:
Input : s = "aba"
Output : 1
Explanation :
The substring "b" (the middle character) is self-contained since 'b' does not appear anywhere else in the string. Thus, the longest self-contained substring has length 1.
Now Your Turn!
Pick the correct output for the given inputInput : s = "ababa"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 ≤ s.length ≤ 5 × 10⁴
- s consists only of lowercase English letters.