The Find Longest Self-Contained Substring coding problem asks you to find the longest substring such that every character present in that substring appears only within that substring in the entire string. Additionally, the substring must be shorter than the whole string. If no such substring exists, return 0 (or -1).
Amazon use this "Hard" problem to test a candidate's ability to analyze global constraints within local windows. It requires a sophisticated understanding of String and Interval interview patterns. You need to know the first and last occurrences of every character to determine the "minimum boundaries" of a valid segment. It evaluations your ability to use Greedy expansion and range merging.
This problem involves Interval Precomputation and Expansion.
first[c] and last[c] index for every character 'a'-'z'.c, consider the range [first[c], last[c]] as a potential starting point.d is found within the range, the range must expand to include last[d] and potentially first[d].[L, R] contains only characters whose entire occurrences are within [L, R], it is a "self-contained" candidate.String: "abacaba"
"abcde", any single character would be length 1.Think of characters as intervals. A self-contained substring is a union of character intervals that doesn't intersect with the parts of any included interval that lie outside. This is a common pattern in compiler design and string compression algorithms.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Custom Sort String | Medium | Solve | |
| Check if Strings Can be Made Equal With Operations II | Medium | Solve | |
| Valid Anagram | Easy | Solve | |
| Maximum Number of Non-Overlapping Substrings | Hard | Solve | |
| Number of Atoms | Hard | Solve |