Largest Substring Between Two Equal Characters

Updated on 04 June, 2025
Largest Substring Between Two Equal Characters header image

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:

  1. 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.

  2. Traversal and Checking: As we traverse each character ch in the string s:

    • 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 of ch incremented by one (to exclude ch itself) from the current index.
  3. 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.
  4. 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.
  • Example 2 ("abca"):

    • 'a' repeats after two other characters ('b' and 'c'), leading to a maximum substring length of 2.
  • Example 3 ("cbzxy"):

    • No character repeats; therefore, the result is -1.

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
cpp
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 string str as its parameter.

  • Use an unordered_map named indexMap 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. Update maxDistance if this new calculated distance is greater than the existing maxDistance.

    • 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.

java
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, updating maximumDistance 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.

python
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, update max_distance using the formula (index - index map[char] - 1). This tracks the longest span between duplicate characters.
  • 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.

Comments

No comments yet.