You are 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: 0Explanation: The optimal substring here is an empty substring between the two 'a's.
Example 2:
Input: s = "abca"
Output: 2Explanation: The optimal substring here is "bc".
Example 3:
Input: s = "cbzxy"
Output: -1Explanation: There are no characters that appear twice in s.
Constraints:
1 <= s.length <= 300s contains only lowercase English letters.Before attempting this problem, you should be comfortable with:
We need to find two equal characters and maximize the number of characters between them. The straightforward approach checks every pair of indices to see if they contain the same character, then computes the distance between them.
-1 (returned if no matching pair exists).i, iterate through all indices j > i.s[i] == s[j], compute the substring length as j - i - 1.class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
n = len(s)
res = -1
for i in range(n):
for j in range(i + 1, n):
if s[i] == s[j]:
res = max(res, j - i - 1)
return resTo maximize the distance between two equal characters, we want the first occurrence and the last occurrence of each character. By recording both indices for every character, we can compute the maximum span in a single pass through the string, followed by a pass through the recorded data.
class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
res = -1
firstIdx = {}
lastIdx = {}
for i, c in enumerate(s):
if c not in firstIdx:
firstIdx[c] = i
else:
lastIdx[c] = i
for c in lastIdx:
res = max(res, lastIdx[c] - firstIdx[c] - 1)
return resWe only need to track the first occurrence of each character. As we encounter a character again, we can immediately compute the distance from its first occurrence. This approach uses a single hash map and computes the answer in one pass.
class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
char_index = {} # char -> first index
res = -1
for i, c in enumerate(s):
if c in char_index:
res = max(res, i - char_index[c] - 1)
else:
char_index[c] = i
return resSince the input contains only lowercase letters, we can replace the hash map with a fixed-size array of 26 elements. This provides constant-time lookups and slightly better cache performance.
26, initialized to -1 (indicating unseen characters).0-25).-1), compute the distance and update the maximum.class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
firstIdx = [-1] * 26
res = -1
for i, c in enumerate(s):
j = ord(c) - ord('a')
if firstIdx[j] != -1:
res = max(res, i - firstIdx[j] - 1)
else:
firstIdx[j] = i
return resThe substring between two equal characters at indices i and j has length j - i - 1, not j - i. Using j - i includes one of the boundary characters in the count, giving an answer that is one larger than correct.
When no two equal characters exist in the string, the answer should be -1. A common mistake is to initialize the result to 0 and forget to handle the case where no matching pair is found, incorrectly returning 0 instead of -1.
Sign in to join the discussion