Skip to content

KMP & RabinKarp in Strings #248

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add files via upload
  • Loading branch information
pranavsuriya-sr authored Oct 9, 2022
commit 06f16eb09a9de96f1e18e575f2b5a7891e4e916d
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;
}

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;
}