Program to find out the number of pairs of equal substrings in Python

Suppose we are given two strings, both made of lowercase alphabets. We have to find out the number of quadruples (p, q, r, s) satisfying the given conditions ?

  • 0 <= p <= q <= length of the first string.

  • 0 <= r <= s <= length of the second string.

  • The substring starting at index p of the first string and ending at index q of the first string, has to be equal to the substring starting at index r of the second string and ending at index s of the second string.

  • q - p has to be the minimum possible value within all the quadruples satisfying the above.

We have to find out the number of such quadruples.

So, if the input is like firstString = 'hgfn', secondString = 'gfrt', then the output will be 2.

There are two quadruples (1, 1, 0, 0) and (2, 2, 1, 1) that satisfy the conditions and have the minimum value for q - p.

Approach

The key insight is that for minimum length substrings, we need single characters. We find the leftmost occurrence of each character in the first string and the rightmost occurrence in the second string. Then we count characters that appear in both strings with the minimum difference in positions.

Algorithm Steps

To solve this, we will follow these steps ?

  • Create arrays to track leftmost positions in first string and rightmost positions in second string
  • For each character in the first string, record its leftmost position
  • For each character in the second string, record its rightmost position
  • Find the minimum difference between leftmost and rightmost positions
  • Count characters that achieve this minimum difference

Example

Let us see the following implementation to get better understanding ?

def solve(firstString, secondString):
    left = [float('inf')] * 26
    right = [-1] * 26
    res = 0
    mi = float('inf')
    
    # Find leftmost occurrence of each character in first string
    for i, ch in enumerate(firstString):
        left[ord(ch) - ord('a')] = min(left[ord(ch) - ord('a')], i)
    
    # Find rightmost occurrence of each character in second string
    for i, ch in enumerate(secondString):
        right[ord(ch) - ord('a')] = max(right[ord(ch) - ord('a')], i)
    
    # Find minimum difference
    for i in range(26):
        if left[i] != float('inf') and right[i] != -1:
            mi = min(mi, left[i] - right[i])
    
    # Count characters with minimum difference
    for i in range(26):
        if left[i] != float('inf') and right[i] != -1:
            if left[i] - right[i] == mi:
                res += 1
    
    return res

# Test the function
print(solve('hgfn', 'gfrt'))

The output of the above code is ?

2

How It Works

For the example strings 'hgfn' and 'gfrt':

  • Character 'g': leftmost in first string at index 1, rightmost in second string at index 0. Difference = 1 - 0 = 1
  • Character 'f': leftmost in first string at index 2, rightmost in second string at index 1. Difference = 2 - 1 = 1

Both characters have the minimum difference of 1, so the answer is 2.

Conclusion

This algorithm efficiently finds pairs of equal single-character substrings by tracking leftmost and rightmost positions. The time complexity is O(n + m) where n and m are the lengths of the input strings.

Updated on: 2026-03-26T14:35:43+05:30

427 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements