3104. Find Longest Self-Contained Substring π
Description
Given a string s, your task is to find the length of the longest self-contained substring of s.
A substring t of a string s is called self-contained if t != s and for every character in t, it doesn't exist in the rest of s.
Return the length of the longest self-contained substring of s if it exists, otherwise, return -1.
Example 1:
Input: s = "abba"
Output: 2
Explanation:
Let's check the substring "bb". You can see that no other "b" is outside of this substring. Hence the answer is 2.
Example 2:
Input: s = "abab"
Output: -1
Explanation:
Every substring we choose does not satisfy the described property (there is some character which is inside and outside of that substring). So the answer would be -1.
Example 3:
Input: s = "abacd"
Output: 4
Explanation:
Let's check the substring "abac". There is only one character outside of this substring and that is "d". There is no "d" inside the chosen substring, so it satisfies the condition and the answer is 4.
Constraints:
2 <= s.length <= 5 * 104sconsists only of lowercase English letters.
Solutions
Solution 1: Enumeration
Thinking
A self-contained substring must contain every occurrence of each of its characters and cannot be the whole string. Checking every interval against \(26\) first/last positions is \(O(n^2|\Sigma|)\) and too heavy.
A valid left endpoint must be some character's first occurrence: extending further left only adds a new character or an earlier copy. With at most \(26\) letters the candidate starts are few.
Record each character's first and last index, enumerate left endpoint \(i\), and walk \(j\) rightward while tracking the farthest cover \(mx\). Stop if a character first appears before \(i\); when \(mx=j\) and the interval is proper, update the longest length.
We notice that the start of a substring that meets the conditions must be the position where a character appears for the first time.
Therefore, we can use two arrays or hash tables first and last to record the positions where each character appears for the first time and the last time, respectively.
Next, we enumerate each character c. Suppose the position where c first appears is \(i\), and the position where it last appears is \(mx\). Then we can start traversing from \(i\). For each position \(j\), we find the position \(a\) where \(s[j]\) first appears and the position \(b\) where it last appears. If \(a < i\), it means that \(s[j]\) is on the left of \(c\), which does not meet the enumeration conditions, and we can exit the loop directly. Otherwise, we update \(mx = \max(mx, b)\). If \(mx = j\) and \(j - i + 1 < n\), we update the answer to \(ans = \max(ans, j - i + 1)\).
Finally, return the answer.
The time complexity is \(O(n \times |\Sigma|)\), and the space complexity is \(O(|\Sigma|)\). Where \(n\) is the length of the string \(s\); and \(|\Sigma|\) is the size of the character set. In this problem, the character set is lowercase letters, so \(|\Sigma| = 26\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |