Skip to content

Commit 9eb7570

Browse files
authored
Create count-pairs-of-equal-substrings-with-minimum-difference.cpp
1 parent a7cbd12 commit 9eb7570

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int countQuadruples(string firstString, string secondString) {
7+
vector<int> lookup1(26, -1);
8+
for (int j = size(firstString) - 1; j >= 0; --j) {
9+
lookup1[firstString[j] - 'a'] = j;
10+
}
11+
vector<int> lookup2(26, -1);
12+
for (int a = 0; a < size(secondString); ++a) {
13+
lookup2[secondString[a] - 'a'] = a;
14+
}
15+
int result = 0, diff = numeric_limits<int>::max();
16+
for (int i = 0; i < 26; ++i) {
17+
if (lookup1[i] == -1 || lookup2[i] == -1) {
18+
continue;
19+
}
20+
if (lookup1[i] - lookup2[i] < diff) {
21+
diff = lookup1[i] - lookup2[i];
22+
result = 0;
23+
}
24+
result += (lookup1[i] - lookup2[i] == diff);
25+
}
26+
return result;
27+
}
28+
};

0 commit comments

Comments
 (0)