-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path06_KMPPatternMatch.cpp
More file actions
210 lines (164 loc) · 7.45 KB
/
Copy path06_KMPPatternMatch.cpp
File metadata and controls
210 lines (164 loc) · 7.45 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
================================================================================
KMP (Knuth-Morris-Pratt) PATTERN MATCHING
================================================================================
PROBLEM: Find all occurrences of pattern in text.
KEY INSIGHT: When a mismatch occurs, use information about the pattern
to skip comparisons that are guaranteed to fail.
CORE CONCEPT: LPS (Longest Proper Prefix which is also Suffix) array
- lps[i] = length of longest proper prefix of pattern[0..i]
that is also a suffix
ALGORITHM:
1. Build LPS array for pattern: O(m)
2. Match pattern against text using LPS to skip: O(n)
TIME: O(n + m)
SPACE: O(m) for LPS array
================================================================================
*/
#include <bits/stdc++.h>
using namespace std;
// ═══════════════════════════════════════════════════════════════════════════
// BUILD LPS (Longest Proper Prefix Suffix) ARRAY
// ═══════════════════════════════════════════════════════════════════════════
vector<int> buildLPS(const string& pattern) {
int m = pattern.size();
vector<int> lps(m, 0);
int len = 0; // Length of previous longest prefix suffix
int i = 1;
while (i < m) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
// Try shorter prefix
len = lps[len - 1];
// Don't increment i
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// ═══════════════════════════════════════════════════════════════════════════
// KMP SEARCH - Find all occurrences
// ═══════════════════════════════════════════════════════════════════════════
vector<int> kmpSearch(const string& text, const string& pattern) {
vector<int> result;
int n = text.size();
int m = pattern.size();
if (m == 0) return result;
vector<int> lps = buildLPS(pattern);
int i = 0; // Index for text
int j = 0; // Index for pattern
while (i < n) {
if (text[i] == pattern[j]) {
i++;
j++;
if (j == m) {
// Pattern found at index (i - j)
result.push_back(i - j);
j = lps[j - 1]; // Look for next match
}
} else {
if (j != 0) {
j = lps[j - 1]; // Use LPS to skip
} else {
i++;
}
}
}
return result;
}
// ═══════════════════════════════════════════════════════════════════════════
// FIND FIRST OCCURRENCE - LC 28: strStr()
// ═══════════════════════════════════════════════════════════════════════════
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
vector<int> lps = buildLPS(needle);
int i = 0, j = 0;
while (i < haystack.size()) {
if (haystack[i] == needle[j]) {
i++;
j++;
if (j == needle.size()) {
return i - j; // First occurrence
}
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return -1; // Not found
}
// ═══════════════════════════════════════════════════════════════════════════
// SHORTEST PALINDROME (LC 214) - KMP application
// ═══════════════════════════════════════════════════════════════════════════
string shortestPalindrome(string s) {
// Find longest palindrome prefix, then prepend reverse of suffix
// Use KMP: find longest prefix of s that matches suffix of reverse(s)
string rev = s;
reverse(rev.begin(), rev.end());
string combined = s + "#" + rev;
vector<int> lps = buildLPS(combined);
int longestPalindromePrefixLen = lps.back();
string toAdd = s.substr(longestPalindromePrefixLen);
reverse(toAdd.begin(), toAdd.end());
return toAdd + s;
}
// ═══════════════════════════════════════════════════════════════════════════
// MAIN
// ═══════════════════════════════════════════════════════════════════════════
int main() {
// Test LPS array
string pattern = "AABAACAABAA";
vector<int> lps = buildLPS(pattern);
cout << "Pattern: " << pattern << "\n";
cout << "LPS: ";
for (int x : lps) cout << x;
cout << "\n\n";
// Test KMP search
string text = "AABAACAADAABAABA";
pattern = "AABA";
cout << "Text: " << text << "\n";
cout << "Pattern: " << pattern << "\n";
cout << "Found at indices: ";
for (int idx : kmpSearch(text, pattern)) {
cout << idx << " ";
}
cout << "\n\n";
// Test strStr
cout << "strStr(\"hello\", \"ll\"): " << strStr("hello", "ll") << "\n";
cout << "strStr(\"aaaaa\", \"bba\"): " << strStr("aaaaa", "bba") << "\n\n";
// Test shortest palindrome
cout << "Shortest palindrome of \"aacecaaa\": " << shortestPalindrome("aacecaaa") << "\n";
cout << "Shortest palindrome of \"abcd\": " << shortestPalindrome("abcd") << "\n";
return 0;
}
/*
================================================================================
LPS ARRAY WALKTHROUGH
================================================================================
Pattern: "AABAACAABAA"
Index: 0 1 2 3 4 5 6 7 8 9 10
Char: A A B A A C A A B A A
LPS: 0 1 0 1 2 0 1 2 3 4 5
lps[5] = 0 because "AABAA" + C has no proper prefix = suffix
lps[10] = 5 because "AABAACAABAA" has prefix "AABAA" = suffix "AABAA"
WHY KMP IS O(n + m):
- LPS build: O(m) - each char compared at most twice
- Search: O(n) - text pointer never goes backward
- Total: O(n + m)
RELATED PROBLEMS:
- LC 28: Find the Index of the First Occurrence in a String
- LC 214: Shortest Palindrome
- LC 459: Repeated Substring Pattern
- LC 1392: Longest Happy Prefix
================================================================================
*/