forked from RajwardhanShinde/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRepeatedStringMatch.cpp
43 lines (37 loc) · 975 Bytes
/
RepeatedStringMatch.cpp
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
class Solution {
public:
void computeLPSArray(string pat, int m, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while(i < m) {
if(pat[i] == pat[len])
lps[i++] = ++len;
else {
if(len == 0) {
lps[i] = 0;
i++;
} else
len = lps[len - 1];
}
}
}
int repeatedStringMatch(string A, string B) {
int n = A.size();
int m = B.size();
int lps[m];
computeLPSArray(B, m, lps);
int i = 0, j = 0;
while(i < n) {
while(j < m && A[(i + j) % n] == B[j]) {
j++;
}
if(j == m)
return ceil((float)(i + j) / n);
i++;
if(j > 0)
j = lps[j - 1];
}
return -1;
}
};