forked from kothariji/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementstrStr-Rabin-Karp.cpp
41 lines (38 loc) · 1.1 KB
/
ImplementstrStr-Rabin-Karp.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
class Solution {
public:
int strStr(string source, string target) {
// Rabin Karp algorithm
int m = source.length();
int n = target.length();
if(m < n)
return -1;
long preCompute = 1;
long hCode = 0;
long tCode = 0;
long d = 256;
long q = 101;
for(int i = 0; i < n - 1; i++)
preCompute = (preCompute * d) % q;
for(int j = 0; j < n; j++) {
hCode = (hCode * d + source[j]) % q;
tCode = (tCode * d + target[j]) % q;
}
for(int i = 0; i <= m - n; i++) {
if(hCode == tCode) {
int j = 0;
for(; j < n; j++) {
if(source[i+j] != target[j])
break;
}
if(j == n)
return i;
}
if(i < m - n) {
hCode = (d * (hCode - source[i] * preCompute) + source[i + n]) % q;
if(hCode < 0)
hCode += q;
}
}
return -1;
}
};