Count Substrings That Differ by One Character

Given two strings s and t, find the number of ways you can choose a non-empty substring of s and replace a single character by a different character such that the resulting substring is a substring of t. In other words, find the number of substrings in s that differ from some substring in t by exactly one character.

Return the number of substrings that satisfy the condition above.

A substring is a contiguous sequence of characters within a string.

Example 1
Inputs = "aba", t = "baba"
Output6
There are 6 pairs of substrings from s and t that differ by exactly one character.
Example 2
Inputs = "ab", t = "bb"
Output3
There are 3 pairs of substrings from s and t that differ by exactly one character.

Constraints

  • 1 <= s.length, t.length <= 100
  • s and t consist of lowercase English letters only.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate