leetcode 1638 - Count Substrings That Differ by One Character

https://leetcode.com/problems/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 “computer” and “computation” 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:
(“aba”, “baba”)
(“aba”, “baba”)
(“aba”, “baba”)
(“aba”, “baba”)
(“aba”, “baba”)
(“aba”, “baba”)
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:
(“ab”, “bb”)
(“ab”, “bb”)
(“ab”, “bb”)
​​​​The underlined portions are the substrings that are chosen from s and t.
Example 3:
Input: s = “a”, t = “a”
Output: 0
Example 4:

Input: s = “abe”, t = “bbc”
Output: 10

Constraints:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
struct TrieNode {
TrieNode *children[26];
int count;

TrieNode() {
for (int i = 0; i < 26; i++)
children[i] = nullptr;
count = 0;
}
};

class Solution {
public:
int countSubstrings(string s, string t) {
int size = t.size();
TrieNode root;

for (int i = 0; i < size; i++)
insert(&root, t.substr(i));

size = s.size();
int ans = 0;

for (int i = 0; i < size; i++)
ans += dfs(&root, s.substr(i), 0, 0);

return ans;
}

void insert(TrieNode *root, string s) {
TrieNode *node = root;

for (char c : s) {
int i = c - 'a';

if (node->children[i] == nullptr)
node->children[i] = new TrieNode;

node = node->children[i];
node->count++;
}
}

int dfs(TrieNode *root, string s, int i, int differ) {
if (root == nullptr) return 0;
if (differ > 1) return 0;
if (i == (int)s.size()) {
return 0;
}

int total = 0;
int idx = s[i] - 'a';

for (int j = 0; j < 26; j++) {
total += dfs(root->children[j], s, i+1, differ + !(j == idx));
if (root->children[j] && differ + !(j == idx) == 1)
total += root->children[j]->count;
}

return total;
}
};

//DP
class Solution {
public:
int countSubstrings(string s, string t) {
int size1 = s.size(), size2 = t.size();

int dp[size1+1][size2+1][2];
int ans = 0;

for (int i = 0; i <= size1; i++) {
for (int j = 0; j <= size2; j++) {
if (i == 0 || j == 0) {
dp[i][j][0] = 0;
dp[i][j][1] = 0;
} else if (s[i-1] == t[j-1]) {
dp[i][j][0] = dp[i-1][j-1][0] + 1;
dp[i][j][1] = dp[i-1][j-1][1];
} else {
dp[i][j][0] = 0;
dp[i][j][1] = dp[i-1][j-1][0] + 1;
}

ans += dp[i][j][1];
}
}

return ans;
}
};