Count Substrings That Differ by One Character
Description
Given two strings s and t, return the count of non-empty substrings of s and t that differ by exactly one character.
Examples
s = "aba", t = "baba"6For each starting position pair (i in s, j in t), expand outward from a mismatch point to count valid substrings. The count is (left_matches + 1) * (right_matches + 1), where left_matches and right_matches are consecutive matching characters on each side of the single differing position.
s = "a", t = "a"0Edge case with a single character.
s = "abc", t = "def"9Since no characters match between s and t, every possible substring pair of the same length will differ by exactly one character. The valid pairs are: (a,d), (b,e), (c,f), (ab,de), (bc,ef), (ac,de), (ab,ef), (abc,def), totaling 9 pairs where each differs by exactly one character.
Constraints
- •
1 ≤ s.length, t.length ≤ 100
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.