-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1638-CountSubstringsThatDifferByOneCharacter.go
More file actions
86 lines (76 loc) · 2.79 KB
/
1638-CountSubstringsThatDifferByOneCharacter.go
File metadata and controls
86 lines (76 loc) · 2.79 KB
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
package main
// 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 "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.
// Constraints:
// 1 <= s.length, t.length <= 100
// s and t consist of lowercase English letters only.
import "fmt"
func countSubstrings(s string, t string) int {
res := 0
helper := func(s, t string, i, j int) int {
res, pre, curr := 0, 0, 0
for sn, tn := len(s), len(t); i < sn && j < tn; i, j = i + 1, j + 1 {
curr++
if s[i] != t[j] {
pre = curr
curr = 0
}
res += pre
}
return res
}
for i := 0; i < len(s); i++ {
res += helper(s, t, i, 0)
}
for j := 1; j < len(t); j++ {
res += helper(s, t, 0, j)
}
return res
}
func main() {
// 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.
fmt.Println(countSubstrings("aba","baba")) // 6
// 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.
fmt.Println(countSubstrings("ab","bb")) // 3
}