Problem 838
Count Substrings That Differ by One Character
Medium · 20 pts · String, Dynamic Programming, Enumeration
Problem
Count the pairs (a substring of S, a substring of T) of the same length that differ in exactly one position. Substrings are counted by position, so equal strings at different places count separately. For every pair of starting points (i, j), walk forward along both strings together, counting mismatches; each step with exactly one mismatch so far adds one pair. This takes O(|S| × |T| × min length), which is fine for strings of length 100.
Input
The first line contains S. The second line contains T. Both use lowercase letters.
Output
Print the number of such pairs.
Constraints
1 <= |S|, |T| <= 100
Example input
ab bb
Example output
3
Explanation
For S = ab and T = bb, the letter a pairs with each of the two b's of T (2 pairs), and ab pairs with bb (1 pair). The b of S matches the b's exactly, so it never differs by one. The answer is 3.