Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "aa"
Output: 0
Explanation: The optimal substring here is an empty substring between the two 'a's.
Example 2:
Input: s = "abca" Output: 2 Explanation: The optimal substring here is "bc".
Example 3:
Input: s = "cbzxy" Output: -1 Explanation: There are no characters that appear twice in s.
Constraints:
1 <= s.length <= 300s contains 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 means we'll examine every possible substring of the given string. For each substring, we will check if it meets the criteria to be the largest substring between two identical characters. We will keep track of the largest one found so far and eventually return that.
Here's how the algorithm would work step-by-step:
def longest_substring_between_equal_characters_brute_force(input_string):
longest_substring_length = -1
for first_index in range(len(input_string)):
for second_index in range(first_index + 1, len(input_string)):
# Check for substrings between equal chars
if input_string[first_index] == input_string[second_index]:
current_substring_length = second_index - first_index - 1
# Store largest substring length between
if current_substring_length > longest_substring_length:
longest_substring_length = current_substring_length
return longest_substring_lengthTo find the largest substring between equal characters quickly, we'll record where we first see each character. Then, as we go through the string, we check if we've seen the character before and, if so, calculate the length between its first and last appearance.
Here's how the algorithm would work step-by-step:
def largest_substring_between_equal_characters(input_string):
first_occurrence = {}
maximum_length = -1
for index, character in enumerate(input_string):
# Store the first index at which char appears
if character not in first_occurrence:
first_occurrence[character] = index
# Update max length if necessary
else:
substring_length = index - first_occurrence[character] - 1
maximum_length = max(maximum_length, substring_length)
# Return result after iterating through the input
return maximum_length| Case | How to Handle |
|---|---|
| Null or empty input string | Return -1 immediately as no substring can exist. |
| String with only one character | Return -1 because a substring between two equal characters requires at least two characters. |
| String with two equal characters at the beginning and end (e.g., 'aa') | The correct length is 0 as the substring between them is empty. |
| String with no repeating characters | Return -1 as there's no valid substring. |
| String with all identical characters (e.g., 'aaaa') | Return the length of the string minus 2, which would be the length between the first and last character. |
| String with a large number of characters to consider performance. | The solution should use a map or array to store first occurrences for O(n) time complexity. |
| String containing unicode or special characters | The solution should correctly handle extended ASCII or Unicode characters in the string. |
| String where the largest substring is at the beginning or end. | Iterate through the string, keeping track of the max length substring so far, ensuring to catch substrings at both ends. |