-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_1638_CountSubstringsThatDifferByOneCharacter.java
72 lines (56 loc) · 2.44 KB
/
_1638_CountSubstringsThatDifferByOneCharacter.java
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
package io.github.tahanima.leetcode;
/**
* @author tahanima
*/
public class _1638_CountSubstringsThatDifferByOneCharacter {
public int[][] computeMatchedCharsFromLeft(String s, String t, int sizeOfS, int sizeOfT) {
int[][] noOfMatchedCharsFromLeft = new int[sizeOfS][sizeOfT];
for (int i = 0; i < sizeOfS; i++) {
for (int j = 0; j < sizeOfT; j++) {
if(s.charAt(i) == t.charAt(j)) {
noOfMatchedCharsFromLeft[i][j] = 1;
if (i > 0 && j > 0) {
noOfMatchedCharsFromLeft[i][j] += noOfMatchedCharsFromLeft[i - 1][j - 1];
}
}
}
}
return noOfMatchedCharsFromLeft;
}
public int[][] computeMatchedCharsFromRight(String s, String t, int sizeOfS, int sizeOfT) {
int[][] noOfMatchedCharsFromRight = new int[sizeOfS][sizeOfT];
for (int i = sizeOfS - 1; i >= 0 ; i--) {
for (int j = sizeOfT - 1; j >= 0; j--) {
if(s.charAt(i) == t.charAt(j)) {
noOfMatchedCharsFromRight[i][j] = 1;
if (i < (sizeOfS - 1) && j < (sizeOfT - 1)) {
noOfMatchedCharsFromRight[i][j] += noOfMatchedCharsFromRight[i + 1][j + 1];
}
}
}
}
return noOfMatchedCharsFromRight;
}
public int countSubstrings(String s, String t) {
int sizeOfS = s.length();
int sizeOfT = t.length();
int[][] noOfMatchedCharsFromLeft = computeMatchedCharsFromLeft(s, t, sizeOfS, sizeOfT);
int[][] noOfMatchedCharsFromRight = computeMatchedCharsFromRight(s, t, sizeOfS, sizeOfT);
int answer = 0;
for (int i = 0; i < sizeOfS; i++) {
for (int j = 0; j < sizeOfT; j++) {
if (s.charAt(i) != t.charAt(j)) {
int totalCombination = 1;
if (((i - 1) >= 0) && ((j - 1) >= 0) && noOfMatchedCharsFromLeft[i - 1][j - 1] > 0) {
totalCombination *= 1 + noOfMatchedCharsFromLeft[i - 1][j - 1];
}
if (((i + 1) < sizeOfS) && ((j + 1) < sizeOfT) && noOfMatchedCharsFromRight[i + 1][j + 1] > 0) {
totalCombination *= 1 + noOfMatchedCharsFromRight[i + 1][j + 1];
}
answer += totalCombination;
}
}
}
return answer;
}
}