Count The Repetitions LeetCode solution in C++

1
class Solution {
2
public:
3
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
4
vector<int> countr(s2.size(), -1), indexr(s2.size(), -1);
5
int count = 0;
6
vector<int> seqIndex;
7
for (int i = 0, j2 = 0; i < n1; i++) {
8
if (indexr[j2] > -1) {
9
int repC = count - countr[j2];
10
int repN = i - indexr[j2];
11
int remainingN = n1 - i;
12
// the times that the pattern repeated
13
count += remainingN / repN * repC;
14
// the residue at the end of str1, but still might form valid s2
15
int j2_ = seqIndex[indexr[j2] + remainingN % repN];
16
count += countr[j2_] - countr[j2];
17
break;
18
} else {
19
countr[j2] = count;
20
indexr[j2] = i;
21
seqIndex.push_back(j2);
22
}
23

24
for (int j1 = 0; j1 < s1.size(); j1++) {
25
if (s1[j1] == s2[j2]) {
26
j2++;
27
if (j2 == s2.size()) {
28
j2 = 0;
29
count++;
30
}
31
}
32
}
33
}
34
return count / n2;
35
}
36
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0