You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.
Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa" Output: 2 Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa". It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef" Output: -1 Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba" Output: 1 Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba". It can be shown that the maximum length achievable is 1.
Constraints:
3 <= s.length <= 5 * 105s consists of only lowercase English letters.In this approach, we traverse through the string while keeping track of the maximum length of 'special' substrings (repeating characters) using a map to store occurrences. We match and compare these substrings to ensure they appear at least three times in the original string and track the maximum possible length that satisfies these conditions. Use a map or array to count substrings efficiently.
The implementation partitions the string into potential special substrings, counts each segment as it traverses, and checks subsequential matches of single characters substring. Using an integer array, we captured the longest reachable substring length that appears at least three times. The final answer represents the maximum such valid substring.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the length of the string, as you only pass through the string once.
Space Complexity: O(1), since the space only grows based on the number of different characters, which is constant in the English alphabet.
This approach leverages the sliding window pattern with an added dynamic counting mechanism to find potential substrings that recur thrice, making sure to handle easy reset and expansion of the window by simply expanding or shrinking based on repetition within the window. The algorithm attempts to incrementally build substrings while reverting or advancing the primary indx controller through appropriately bounded checks.
This C program navigates through the string using a sliding window approach where each window attempts to capture conjunct repeated characters limited to three-time minimal requirements. For each triple detection, progress through the string resets within conditions allowing consecutive suffix captures up to further elongation where potential and maximum length are constantly updated.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), as the mechanism ensures each character is analysed twice, one for window initialization and expansion through potential termination.
Space Complexity: O(1), attributed to static space limit.
| Approach | Complexity |
|---|---|
| Approach 1: Linear Scan with Map Tracking | Time Complexity: O(n), where n is the length of the string, as you only pass through the string once. |
| Approach 2: Sliding Window Technique with Dynamic Counting | Time Complexity: O(n), as the mechanism ensures each character is analysed twice, one for window initialization and expansion through potential termination. |
Find Longest Special Substring That Occurs Thrice I & II | Leetcode 2981 & 2982 | codestorywithMIK • codestorywithMIK • 8,401 views views
Watch 9 more video solutions →Practice Find Longest Special Substring That Occurs Thrice II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor