Count Substrings That Differ by One Character - Problem

Imagine you're a text editor developer working on a "smart suggestion" feature. Given two strings s and t, you need to find how many substrings from s can be transformed into substrings of t by changing exactly one character.

More formally, count the number of ways you can:

  • Choose a non-empty substring from s
  • Replace exactly one character in it
  • Make it match some substring in t

For example, in "computer" and "computation", the substring "comput" from the first string differs from "comput" in the second string by exactly one character (position doesn't matter for counting).

Goal: Return the total count of such valid transformations.

Input & Output

example_1.py โ€” Basic Example
$ Input: s = "aba", t = "baba"
โ€บ Output: 6
๐Ÿ’ก Note: The substrings are: "a" from s โ†’ "b" from t (change aโ†’b), "b" from s โ†’ "a" from t (change bโ†’a), "ab" from s โ†’ "ba" from t (change aโ†’b), "ba" from s โ†’ "ab" from t (change bโ†’a), "aba" from s โ†’ "bab" from t (change aโ†’b), "aba" from s โ†’ "aba" from t (change bโ†’a)
example_2.py โ€” Same Characters
$ Input: s = "ab", t = "bb"
โ€บ Output: 3
๐Ÿ’ก Note: The valid transformations are: "a" โ†’ "b" (change aโ†’b), "ab" โ†’ "bb" (change aโ†’b), "b" โ†’ "b" is not valid since no change needed
example_3.py โ€” Single Character
$ Input: s = "a", t = "a"
โ€บ Output: 0
๐Ÿ’ก Note: No valid substrings because "a" and "a" are identical, so we cannot make exactly one character change

Visualization

Tap to expand
Visual Solution OverviewStep-by-Step Process1Find Diff2Count Left3Count Right4MultiplyExample: s="computer", t="computation"Position 5: 't' vs 'a'Left matches: 5 chars ("compu") โ†’ 6 possible start positionsRight matches: 0 chars โ†’ 1 possible end positionTotal substrings: 6 ร— 1 = 6 different valid transformationsTime: O(nm), Space: O(1) - Much better than brute force O(nยฒmยฒk)!
Understanding the Visualization
1
Identify difference positions
Find all positions where the two strings have different characters
2
Count matching neighbors
For each difference, count how many characters match before and after
3
Calculate combinations
Multiply prefix and suffix counts to get total valid substrings
4
Sum all possibilities
Add up counts from all difference positions
Key Takeaway
๐ŸŽฏ Key Insight: Instead of generating all substrings, we fix each difference position and count how many valid substrings can be formed around it using mathematical combinations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nm)

We process each character pair once, where n and m are string lengths

n
2n
โœ“ Linear Growth
Space Complexity
O(nm)

Two DP arrays to store prefix and suffix matching lengths

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length, t.length โ‰ค 100
  • s and t consist of lowercase English letters only
  • Must differ by exactly one character - not zero, not two or more
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen