Skip to content

Commit a972a11

Browse files
authored
Merge pull request #3582 from sjain1909/t9
Adding solution of KMP algorithm
2 parents 89687be + a5ac7e2 commit a972a11

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

dsa/Advance/knuth-morris-pratt.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
id: knuth-morris-pratt
3+
title: Knuth-Morris-Pratt (KMP) Algorithm
4+
sidebar_label: 0006 - Knuth-Morris-Pratt Algorithm
5+
tags: [KMP, String Matching, Algorithm, C++, Problem Solving]
6+
description: This is a solution for implementing the Knuth-Morris-Pratt (KMP) algorithm for efficient substring searching and pattern matching.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
The Knuth-Morris-Pratt (KMP) algorithm is an efficient string searching algorithm that improves the performance of substring searches within a main string. The algorithm preprocesses the pattern to create a partial match table (also known as the "lps" array), which is used to skip unnecessary comparisons during the search process.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
```plaintext
20+
Input:
21+
Text: "abxabcabcaby"
22+
Pattern: "abcaby"
23+
Output:
24+
Pattern found at index 6
25+
```
26+
27+
### Constraints
28+
29+
- The length of the text and the pattern can be up to 10^5.
30+
31+
## Solution of Given Problem
32+
33+
### Intuition and Approach
34+
35+
The KMP algorithm follows these steps:
36+
37+
1. Preprocessing the Pattern: Compute the longest proper prefix which is also a suffix (lps) array.
38+
2. Searching the Text: Use the lps array to skip characters in the text while matching the pattern.
39+
40+
### Approaches
41+
42+
#### Codes in Different Languages
43+
44+
<Tabs>
45+
<TabItem value="cpp" label="C++">
46+
<SolutionAuthor name="sjain1909"/>
47+
```cpp
48+
#include <bits/stdc++.h>
49+
using namespace std;
50+
void computeLPSArray(string& pat, int M, vector<int>& lps) {
51+
int length = 0;
52+
lps[0] = 0;
53+
int i = 1;
54+
55+
while (i < M) {
56+
if (pat[i] == pat[length]) {
57+
length++;
58+
lps[i] = length;
59+
i++;
60+
} else {
61+
if (length != 0) {
62+
length = lps[length - 1];
63+
} else {
64+
lps[i] = 0;
65+
i++;
66+
}
67+
}
68+
}
69+
}
70+
71+
void KMPSearch(string& pat, string& txt) {
72+
int M = pat.length();
73+
int N = txt.length();
74+
75+
vector<int> lps(M);
76+
77+
computeLPSArray(pat, M, lps);
78+
79+
int i = 0;
80+
int j = 0;
81+
while (i < N) {
82+
if (pat[j] == txt[i]) {
83+
j++;
84+
i++;
85+
}
86+
87+
if (j == M) {
88+
cout << "Pattern found at index " << i - j << "\n";
89+
j = lps[j - 1];
90+
} else if (i < N && pat[j] != txt[i]) {
91+
if (j != 0) {
92+
j = lps[j - 1];
93+
} else {
94+
i++;
95+
}
96+
}
97+
}
98+
}
99+
100+
int main() {
101+
string txt, pat;
102+
cout << "Enter the text: ";
103+
cin >> txt;
104+
cout << "Enter the pattern: ";
105+
cin >> pat;
106+
107+
KMPSearch(pat, txt);
108+
109+
return 0;
110+
}
111+
```
112+
</TabItem>
113+
</Tabs>
114+
115+
### Complexity Analysis
116+
117+
- **Time Complexity:** $O(N + M)$ where N is the length of the text and M is the length of the pattern.
118+
- **Space Complexity:** $O(M)$ for the lps array.
119+
120+
## Video Explanation of Given Problem
121+
122+
<LiteYouTubeEmbed
123+
id="obWXjtg0L64"
124+
params="autoplay=1&autohide=1&showinfo=0&rel=0"
125+
title="Problem Explanation | Solution | Approach"
126+
poster="maxresdefault"
127+
webp
128+
/>
129+
---
130+
131+
<h2>Authors:</h2>
132+
133+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
134+
{['sjain1909'].map(username => (
135+
<Author key={username} username={username} />
136+
))}
137+
</div>

0 commit comments

Comments
 (0)