Program to count substrings that differ by one character in Python

Suppose we have two strings s and t, we have to find the number of ways we can select a nonempty substring of s and replace one single character by another different character such that the resulting substring is one of the substring of t. We have to find the number of substrings that satisfy the condition above.

So, if the input is like s = "sts" t = "tsts", then the output will be 6 because the following are the pairs of substrings from s and t that differ by 1 character −

  • ("s", "t") − position 0 in s vs position 0 in t
  • ("st", "ts") − position 0-1 in s vs position 0-1 in t
  • ("sts", "tst") − position 0-2 in s vs position 0-2 in t
  • ("t", "s") − position 1 in s vs position 1 in t
  • ("ts", "st") − position 1-2 in s vs position 1-2 in t
  • ("s", "s") − position 2 in s vs position 3 in t (actually "s" vs "t" at different positions)

Algorithm

To solve this problem, we will follow these steps −

  • Iterate through each starting position in both strings
  • For each pair of starting positions, find matching prefixes
  • If characters differ at some position, count all possible substring pairs that differ by exactly that one character
  • Continue matching after the differing character to count extended substrings

Example

Let us see the following implementation to get better understanding −

def solve(s, t):
    n1 = len(s)
    n2 = len(t)
    ans = 0

    for i1 in range(n1):
        for i2 in range(n2):
            i = i1
            j = i2

            # Skip matching characters at the beginning
            while i < n1 and j < n2 and s[i] == t[j]:
                i += 1
                j += 1

            # If we find a differing character
            if i < n1 and j < n2 and s[i] != t[j]:
                i += 1
                j += 1
                ans += 1
                
                # Count all extended substrings that continue to match
                while i < n1 and j < n2 and s[i] == t[j]:
                    i += 1
                    j += 1
                    ans += 1

    return ans

# Test the function
s = "sts"
t = "tsts"
result = solve(s, t)
print(f"Number of substrings that differ by one character: {result}")
Number of substrings that differ by one character: 6

How It Works

The algorithm works by examining every possible pair of starting positions in both strings. For each pair:

  1. It first skips over any matching characters at the beginning
  2. When it encounters the first differing character, it counts this as one valid substring pair
  3. It then continues to match subsequent characters, counting each extended matching substring

This approach ensures we count all possible substrings that differ by exactly one character between the two input strings.

Conclusion

This solution uses a nested loop approach with O(n1 × n2 × max(n1, n2)) time complexity to count all substring pairs that differ by exactly one character. The algorithm efficiently handles the constraint by ensuring each counted pair has exactly one character difference.

Updated on: 2026-03-26T13:52:20+05:30

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements