-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnuth_morris_pratt_Algo.cpp
54 lines (46 loc) · 1.26 KB
/
Knuth_morris_pratt_Algo.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
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
vector<int> compute_pie_table(string needle, int m) {
vector<int> pieTable(m, 0);
int k = 0;
for (int i = 1; i < m; i++) {
while (k > 0 && needle[k] != needle[i]) {
k = pieTable[k - 1];
}
if (needle[k] == needle[i]) {
k++;
}
pieTable[i] = k;
}
return pieTable;
}
public:
int strStr(string haystack, string needle) {
int n = haystack.size();
int m = needle.size();
if (m == 0) return 0; // Handle empty needle edge case
vector<int> pieTable = compute_pie_table(needle, m);
int j = 0;
for (int i = 0; i < n; i++) {
while (j > 0 && needle[j] != haystack[i]) {
j = pieTable[j - 1];
}
if (needle[j] == haystack[i]) {
j++;
if (j == m) {
return i - m + 1; // Return starting index of match
}
}
}
return -1; // No match found
}
};
int main() {
Solution obj;
string haystack = "hello";
string needle = "ll";
cout << obj.strStr(haystack, needle);
return 0;
}