
Problem Statement
In this problem, we are given a string s
and need to determine the length of the longest substring that lies between two identical characters. Importantly, these two characters should not be included in the substring length count. If no two characters in the string are the same, we should return -1
. Our goal is to systematically find this substring length by analyzing the positions of repeating characters in the string s
.
Examples
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 <= 300
s
contains only lowercase English letters.
Approach and Intuition
The strategy for finding the longest substring between two equal characters can be conceptualized as follows:
Initialization of Storage: Utilize a dictionary to store the first occurrence of each character when it is encountered in the string. This helps in determining when a character repeats.
Traversal and Checking: As we traverse each character
ch
in the strings
:- If
ch
does not exist in our dictionary, add it with its current index. - If
ch
is found in our dictionary, calculate the substring length by subtracting the stored index ofch
incremented by one (to excludech
itself) from the current index.
- If
Distance Calculation: Each time a repeating character is encountered:
- The length of the substring between the two appearances could be found by
current_index - first_seen_index_of_character - 1
. - Compare this with a running maximum to keep track of the longest length encountered.
- The length of the substring between the two appearances could be found by
Final Answer: Once the string is fully traversed, check if the maximum length found is greater than
-1
. If it is, this length is our answer. Otherwise, return-1
.
Insights from Examples
Example 1 ("aa"):
- The characters are the same and directly next to each other, resulting in a substring length of
0
.
- The characters are the same and directly next to each other, resulting in a substring length of
Example 2 ("abca"):
- 'a' repeats after two other characters ('b' and 'c'), leading to a maximum substring length of
2
.
- 'a' repeats after two other characters ('b' and 'c'), leading to a maximum substring length of
Example 3 ("cbzxy"):
- No character repeats; therefore, the result is
-1
.
- No character repeats; therefore, the result is
Following these steps ensures that we approach the problem systematically, taking into account all positions of characters and only calculating substrings where characters repeat in the given string.
Solutions
- C++
- Java
- Python
class Solution {
public:
int maxDistanceBetweenSameChars(string str) {
unordered_map<char, int> indexMap;
int maxDistance = -1;
for (int idx = 0; idx < str.size(); idx++) {
if (indexMap.count(str[idx])) {
maxDistance = max(maxDistance, idx - indexMap[str[idx]] - 1);
} else {
indexMap[str[idx]] = idx;
}
}
return maxDistance;
}
};
This solution in C++ focuses on finding the maximum distance (number of characters between two same characters) between two identical characters in a string. Here’s how this code achieves the solution:
Initially, define a helper function
maxDistanceBetweenSameChars
that takes a stringstr
as its parameter.Use an
unordered_map
namedindexMap
to store characters from the string and their first occurring index.Initialize an integer
maxDistance
to -1 to handle cases where no two characters are the same.Iterate over each character of the string using a for-loop. During each iteration:
If the character is already present in
indexMap
, calculate the distance between its first occurrence (stored in the map) and the current index. UpdatemaxDistance
if this new calculated distance is greater than the existingmaxDistance
.If the character is not present in the map, add the character and its index to the
indexMap
.
After completing the loop, return the value of
maxDistance
, which represents the largest number of characters between the first and last occurrence of any character that appears more than once.
This approach ensures an efficient computation by using a hash map to store indices, allowing for constant time complexity checks and updates. The final returned value from the function provides the required maximum distance.
class Solution {
public int maxDistanceBetweenSameChars(String input) {
Map<Character, Integer> indexMap = new HashMap<>();
int maximumDistance = -1;
for (int index = 0; index < input.length(); index++) {
char currentChar = input.charAt(index);
if (indexMap.containsKey(currentChar)) {
maximumDistance = Math.max(maximumDistance, index - indexMap.get(currentChar) - 1);
} else {
indexMap.put(currentChar, index);
}
}
return maximumDistance;
}
}
The provided Java solution helps find the largest substring between two equal characters in a given string. In this approach:
- A HashMap named
indexMap
is utilized to store characters and their first occurrence indices from the string. - An integer
maximumDistance
initializes at -1 to track the maximum length of substrings found. - The program iterates through the input string, examining each character.
- During each iteration, if the character already exists in
indexMap
, the program calculates the distance between the current index and the first occurrence index of that character, updatingmaximumDistance
if this new distance is larger. - If the character is not found in
indexMap
, the current index is saved as its first occurrence. - After completing the loop, the result, held in
maximumDistance
, provides the length of the largest substring between two identical characters.
The solution effectively uses a map to reduce the complexity of tracking and comparing indices, resulting in an efficient approach to solving the problem.
class Solution:
def maxLenBetweenEqualChars(self, string: str) -> int:
index_map = {}
max_distance = -1
for index in range(len(string)):
if string[index] in index_map:
max_distance = max(max_distance, index - index_map[string[index]] - 1)
else:
index_map[string[index]] = index
return max_distance
The given Python code snippet efficiently computes the longest substring distance between two identical characters in a given string. It implements the solution using a dictionary (index_map
) to track the earliest index where each character appears and iteratively calculates the maximum distance.
- Initialize a dictionary
index_map
to store characters and their first index. - Set
max_distance
to-1
as a starting value, considering the instance where no two characters are the same. - Loop through the string by indexing each character:
- If the character exists in
index_map
, updatemax_distance
using the formula(index - index map[char] - 1)
. This tracks the longest span between duplicate characters.
- If the character exists in
- Update
index_map
with the current index of the character when it is encountered for the first time. - Finally, it returns the computed maximum distance which is the length of the longest substring found between two identical characters. If no duplicate character is found, it would return
-1
.
Use this snippet to quickly get the distance of the largest substring situated between two matching characters in any given string.
No comments yet.