{leetcode}/problems/count-substrings-that-differ-by-one-character/[LeetCode - 1638. 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.
For example, the underlined substrings in "[.underline]#compute#r"
and "[.underline]#computa#tion"
only differ by the 'e'
/'a'
, so this is a valid way.
Return the number of substrings that satisfy the condition above.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "aba", t = "baba" Output: 6 Explanation: The following are the pairs of substrings from s and t that differ by exactly 1 character: ("a#ba", "[.underline]#b#aba") ("[.underline]#a#ba", "ba[.underline]#b#a") ("ab[.underline]#a", "b#aba") ("ab[.underline]#a", "ba[.underline]b#a") ("a[.underline]#b#a", "b[.underline]#a#ba") ("a[.underline]#b#a", "bab[.underline]#a") The underlined portions are the substrings that are chosen from s and t.
Example 2:
Input: s = "ab", t = "bb" Output: 3 Explanation: The following are the pairs of substrings from s and t that differ by 1 character: ("a#b", "[.underline]#b#b") ("[.underline]#a#b", "b[.underline]#b") ("ab", "bb") The underlined portions are the substrings that are chosen from s and t.
Constraints:
-
1 ⇐ s.length, t.length ⇐ 100
-
s
andt
consist of lowercase English letters only.