class Solution:
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
first_index = {}
max_len = -1
for i, ch in enumerate(s):
if ch in first_index:
max_len = max(max_len, i - first_index[ch] - 1)
else:
first_index[ch] = i
return max_len
class Solution {
public:
int maxLengthBetweenEqualCharacters(string s) {
unordered_map<char, int> first_index;
int max_len = -1;
for (int i = 0; i < s.size(); ++i) {
char ch = s[i];
if (first_index.count(ch)) {
max_len = max(max_len, i - first_index[ch] - 1);
} else {
first_index[ch] = i;
}
}
return max_len;
}
};
class Solution {
public int maxLengthBetweenEqualCharacters(String s) {
Map<Character, Integer> firstIndex = new HashMap<>();
int maxLen = -1;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (firstIndex.containsKey(ch)) {
maxLen = Math.max(maxLen, i - firstIndex.get(ch) - 1);
} else {
firstIndex.put(ch, i);
}
}
return maxLen;
}
}
var maxLengthBetweenEqualCharacters = function(s) {
let firstIndex = {};
let maxLen = -1;
for (let i = 0; i < s.length; i++) {
let ch = s[i];
if (firstIndex.hasOwnProperty(ch)) {
maxLen = Math.max(maxLen, i - firstIndex[ch] - 1);
} else {
firstIndex[ch] = i;
}
}
return maxLen;
};
Given a string s
, find the length of the largest substring that is located between two equal characters in s
. Specifically, for any two equal characters at different positions in the string, calculate the length of the substring that lies strictly between them (excluding the two characters themselves). If there are no two equal characters, return -1
.
s
contains only lowercase English letters.The problem asks us to find the maximum length between two equal characters in a string. At first glance, a brute-force approach might involve checking every possible pair of characters and calculating the substring length between them. However, this would be inefficient for long strings.
To optimize, we realize that for each character, the largest possible substring is found between its first and last occurrence. This is because the distance between the first and last occurrence is always greater than or equal to the distance between any other pair of identical characters. Therefore, instead of checking all pairs, we can record the first occurrence of each character and, as we iterate through the string, compare it to the current index whenever we see the same character again.
-1
.
-1
will be returned.
We use a dictionary because it allows us to look up the first occurrence of any character in constant time, making the overall algorithm efficient.
Let's consider the input s = "abca"
.
max_len = -1
.3 - 0 - 1 = 2
. Update max_len = 2
.
The answer is 2
because the largest substring between two equal characters ('a' at index 0 and 3) is "bc", which has length 2.
O(n^2)
time complexity, where n
is the length of the string.
O(n)
.
O(1)
(constant).
The key insight is that the largest substring between two equal characters is always found between the first and last occurrence of that character. By keeping track of the first occurrence index for each character and updating the maximum length whenever we see a repeat, we achieve an efficient and elegant solution. This approach avoids unnecessary comparisons and leverages hash maps for quick lookups, resulting in a linear time algorithm suitable for large inputs.