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 <= 50s consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to find the longest special substring appearing thrice involves checking every possible substring of the input string. We generate each substring, count how many times it appears in the original string, and keep track of the longest substring that appears at least three times. Ultimately we check *every* possibility.
Here's how the algorithm would work step-by-step:
def find_longest_special_substring_that_occurs_thrice_i(input_string):
longest_substring_found = ""
string_length = len(input_string)
for substring_length in range(1, string_length + 1):
for starting_index in range(string_length - substring_length + 1):
substring = input_string[starting_index:starting_index + substring_length]
substring_count = 0
# Count the number of occurrences of the current substring
for i in range(string_length - substring_length + 1):
if input_string[i:i + substring_length] == substring:
substring_count += 1
#Check if the substring occurs at least thrice
if substring_count >= 3:
# Update the longest substring if the current substring is longer
if substring_length > len(longest_substring_found):
longest_substring_found = substring
return longest_substring_foundThe optimal approach is to check substrings of decreasing lengths to find the longest one that appears at least three times. We can do this efficiently by keeping track of the occurrences of substrings and stopping early once we find a valid one.
Here's how the algorithm would work step-by-step:
def find_longest_special_substring_that_occurs_thrice(input_string):
string_length = len(input_string)
for substring_length in range(string_length - 1, 0, -1):
# Iterate through possible substring lengths
for i in range(string_length - substring_length + 1):
substring = input_string[i:i + substring_length]
count = 0
for j in range(string_length - substring_length + 1):
if input_string[j:j + substring_length] == substring:
count += 1
# If a substring appears thrice, return it
if count >= 3:
return substring
# No substring appearing at least thrice found
return ""| Case | How to Handle |
|---|---|
| Empty string input | Return 0 immediately as there are no substrings to evaluate. |
| String with length less than 3 | Return 0 since a substring must appear at least thrice, which is impossible with fewer than 3 characters. |
| String with only one distinct character that appears fewer than three times | Iterate through the string and count the occurrences; return 0 if the count is less than 3. |
| String with only one distinct character appearing many times (e.g., 'aaaaaaaaaa') | The solution should correctly identify the length of the string as the answer. |
| String with many different characters and no special substrings appearing three times | The solution should iterate through all possible special substrings and correctly return 0. |
| String with some special substrings appearing exactly twice | The solution must correctly ignore these substrings as it only needs to consider substrings appearing at least three times. |
| String with multiple special substrings of the same length, each appearing at least three times | The solution should return the length of any of these longest substrings since they are all valid maximums. |
| Very long string to check performance and avoid Time Limit Exceeded (TLE) | Optimize the search to efficiently count substring occurences; substring search needs to be efficient to avoid TLE for long strings. |