|
| 1 | +# Find Longest Self-Contained Substring |
| 2 | + |
| 3 | +You are given a string, s, consisting of lowercase English letters. Your task is to find the length of the longest |
| 4 | +self-contained substring of s. |
| 5 | + |
| 6 | +A substring t of s is called self-contained if: |
| 7 | +- t is not equal to the entire string s. |
| 8 | +- Every character in t does not appear anywhere else in s (outside of t). |
| 9 | + |
| 10 | +In other words, all characters in t are completely unique to that substring within the string s. |
| 11 | +Return the length of the longest self-contained substring. If no such substring exists, return -1. |
| 12 | + |
| 13 | +Constraints: |
| 14 | + |
| 15 | +- 2 ≤ s.length ≤ 1000 |
| 16 | +- s consists only of lowercase English letters. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +We iterate through the string once to record where each character first appears and where it last appears. Two separate |
| 34 | +hash maps store each character’s first and last occurrence indices. Each unique character serves as a potential starting |
| 35 | +point, defining an initial window from its first to last occurrence. The window is adjusted based on the following |
| 36 | +conditions: |
| 37 | + |
| 38 | +- As we iterate within this window, if we encounter a character whose last occurrence is further to the right than the |
| 39 | +current window’s end, we expand the window to include it. This ensures that the substring remains self-contained, |
| 40 | +meaning all occurrences of each character within it are included. The process continues until no more characters |
| 41 | +extend the window’s boundary. |
| 42 | + |
| 43 | +- As we expand the window, every character inside it must have its first occurrence within the current window’s start |
| 44 | +boundary. If we encounter a character whose first occurrence index is before the current window’s starting position, |
| 45 | +it means that an earlier part of the string contains an instance of that character, violating the self-contained |
| 46 | +property. When this happens, the current substring is invalid, and we discard it from consideration. |
| 47 | + |
| 48 | +The maximum valid window length is tracked and updated accordingly, ensuring the longest valid substring is returned. |
| 49 | + |
| 50 | +The steps of the algorithm are as follows: |
| 51 | + |
| 52 | +1. Create two hashmaps, first, and last, to store the first and last occurrence index of each character in the string. |
| 53 | +2. Iterate through the string once to populate these hash maps with each character’s first and last occurrence. |
| 54 | +3. Initialize max_len = -1 to keep track of the maximum length of a valid self-contained substring found. |
| 55 | +4. For each unique character c1 (processed once), start from its first occurrence index: |
| 56 | + |
| 57 | + - Initialize the start and end of the window by setting the starting point to the character’s first occurrence and the |
| 58 | + ending point to its last occurrence in the string. |
| 59 | + |
| 60 | + - Iterate through the string from the starting position start, extending the endpoint end whenever a character’s last |
| 61 | + occurrence is beyond the current endpoint end. |
| 62 | + |
| 63 | + - If a character c2 inside the window has its first occurrence before the window's start, the window is invalid. |
| 64 | + |
| 65 | +5. Validate the substring: |
| 66 | + |
| 67 | + - When the current index j reaches the end, check if the window is valid and its length is less than the total string |
| 68 | + length. |
| 69 | + |
| 70 | + - If the window is valid, update max_len with the maximum of its current value and the window’s length (end - start + 1). |
| 71 | + |
| 72 | +6. After checking all potential starting characters, return the maximum valid length found. If no valid substring exists, |
| 73 | +return -1. |
| 74 | + |
| 75 | +Let’s look at the illustration below to better understand the solution. |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +### Time Complexity |
| 107 | + |
| 108 | +The time complexity of the above solution is O(n), where n is the number of characters in the string. |
| 109 | + |
| 110 | +### Space Complexity |
| 111 | + |
| 112 | +The space complexity of the above solution is O(1) because of the fixed character set size, 26 lowercase English letters. |
0 commit comments