292. Count Substrings That Differ by One Character

HardStringString

Given two strings s and t, return the number of ways to choose a non-empty substring of s and replace exactly one of its characters so that the resulting substring equals a substring of t (of the same length). In other words, count pairs of equal-length substrings (one from s, one from t) that differ in exactly one position. Input: '"s", "t"'.

Input: '"s", "t"'.

Output: Integer — number of qualifying substring pairs.

Examples

Example 1
Input: "aba", "baba"
Output: 6
Explanation: Six substring pairs differ by exactly one char.
Example 2
Input: "ab", "bb"
Output: 3
Explanation: Three qualifying pairs.
Example 3
Input: "a", "a"
Output: 0
Explanation: No single-char difference.

Constraints

Asked by

Microsoft
Solve this problem in the editor →