Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KMP & RabinKarp in Strings #248

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions String/PatternMatching/KMPalgorithm.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "bits/stdc++.h"
using namespace std;

void findLpsArray(char* pattern, int* lps, int m)
{

int len = 0;

lps[0] = 0;
int i = 1;

while (i < m)
{
if (pattern[i] == pattern[len])
{
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0)
{
len = lps[len - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
}

void patternSearchKMP(char* pattern, char* text)
{
int m = strlen(pattern);
int n = strlen(text);

int lps[m]; //longest-prefix-suffix array

findLpsArray(pattern, lps, m); //calling findLpsArray function

int i = 0;
int j = 0;

while (i < n)
{
if (pattern[j] == text[i])
{
j++;
i++;
}

if (j == m)
{
cout<<"Pattern found at index: "<<i - j;
j = lps[j - 1];
}

else if (i < n && pattern[j] != text[i])
{
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}

int main()
{
char pattern[100], text[100];

cout<<"Enter the pattern to be found: "<<endl;
cin>>pattern;

cout<<"Enter the string in which the pattern is to be found: "<<endl;
cin>>text;

patternSearchKMP(pattern, text); //Function call

return 0;
}

40 changes: 40 additions & 0 deletions String/PatternMatching/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
KMP Algorithm:

The Knuth-Morris-Pratt algorithm is used to find a given text from a specific word.

This algorithm uses the degenerating property which primarily works based on sub-strings.

The basic idea of this algorithm is to not match a character which is already known to match for sure.

Example:

Output: Enter the pattern to be found:

Input: cktober

Output : Enter the string in which the pattern is to be found:

Input: hacktoberfest

Output/Answer: Pattern found at index: 2

Rabin Karp Algorithm

The Rabin karp Algorithm is used to search a text from a given word using hash function.

This algorithm doesnt search slide through every character but filters the unlike characters and then does the comparing.

Example:

Enter the pattern to be found:

program

Enter the string in which the pattern is to be found:

competitveprogramming

Pattern found at index: 10



56 changes: 56 additions & 0 deletions String/PatternMatching/RabinKarpAlgorithm.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "bits/stdc++.h"
using namespace std;

void rabinKarpAlgo(char* text, char* pattern)
{
int m = strlen(text);
int n = strlen(pattern);

int prime = 31;
int mod = 1e9 + 9;

vector<long long> p_pow(m);
p_pow[0] = 1;

for (int i = 1; i < m; i++)
{
p_pow[i] = (p_pow[i-1] * prime) % mod;
}

vector<long long> h(m + 1, 0);

for (int i = 0; i < m; i++)
{
h[i+1] = (h[i] + (text[i] - 'a' + 1) * p_pow[i]) % mod;
}

long long hash_pattern = 0;

for (int i = 0; i < n; i++)
{
hash_pattern = (hash_pattern + (pattern[i] - 'a' + 1) * p_pow[i]) % mod;
}

for (int i = 0; i + n - 1 < m; i++)
{
long long curr_hash = (h[i+n] + mod - h[i]) % mod;

if (curr_hash == hash_pattern * p_pow[i] % mod)
cout<<"Pattern found at index: "<<i<<endl;
}
}

int main()
{
char pattern[100], text[100];

cout<<"Enter the pattern to be found: "<<endl;
cin>>pattern;

cout<<"Enter the string in which the pattern is to be found: "<<endl;
cin>>text;

rabinKarpAlgo(text, pattern); //Function call

return 0;
}