Skip to content

Commit

Permalink
String matching created
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud-Araf committed Sep 30, 2024
1 parent 720e59f commit 6cf1c8f
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.DS_Store
1 change: 1 addition & 0 deletions Special/inversion_counting.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// implementation of invertion counting using merge sort

#include<bits/stdc++.h>
using namespace std;

Expand Down
92 changes: 92 additions & 0 deletions String Matching/kmp_approach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// implementation of kmp approach for string matching
// Time complexity: O(n+m)
// where n is the length of the text and m is the length of the pattern
// Space complexity: O(m)

#include<bits/stdc++.h>
using namespace std;


void computeLPS(string pattern, vector<int>& lps)
{
int m = pattern.length();
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++;
}
}
}
}

vector<int>kmp_approach(string text, string pattern)
{
int n = text.length();
int m = pattern.length();
vector<int> fndIndex;
vector<int> lps(m);

computeLPS(pattern, lps);

int i = 0;
int j = 0;

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

if (j == m)
{
fndIndex.push_back(i - j);
cout << "Pattern found at index " << i - j << endl;
j = lps[j - 1];
}
else if (i < n && pattern[j] != text[i])
{
if (j != 0)
{
j = lps[j - 1];
}
else
{
i++;
}
}
}

return fndIndex;
}

int main()
{
string text = "ababcababcabab";
string pattern = "abc";
vector<int> fndIndex = kmp_approach(text, pattern);
if (fndIndex.size() == 0)
{
cout << "Pattern not found" << endl;
}
return 0;
}
50 changes: 50 additions & 0 deletions String Matching/naive_approach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// implementation of naive approach for string matching
// Time complexity: O(n*m)
// where n is the length of the text and m is the length of the pattern
// Space complexity: O(1)

#include<bits/stdc++.h>
using namespace std;


vector<int> naive_approach(string text, string pattern)
{
int n = text.length();
int m = pattern.length();
vector<int> fndIndex;

for (int i = 0; i <= n - m; i++)
{
int j;
for (j = 0; j < m; j++)
{
if (text[i + j] != pattern[j])
{
break;
}
}

if (j == m)
{
fndIndex.push_back(i);
cout << "Pattern found at index " << i << endl;
}
}

return fndIndex;
}

int main()
{
string text = "ababcababcabab";
string pattern = "abc";

vector<int> fndIndex = naive_approach(text, pattern);

if (fndIndex.size() == 0)
{
cout << "Pattern not found" << endl;
}

return 0;
}
79 changes: 79 additions & 0 deletions String Matching/robin_karp_approach.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// implementation of robin karp approach for string matching
// Time complexity: O(n+m)
// where n is the length of the text and m is the length of the pattern
// Space complexity: O(1)

#include<bits/stdc++.h>
using namespace std;

#define d 256
#define q INT_MAX

vector<int> robin_karp(string text, string pattern)
{
int n = text.length();
int m = pattern.length();
vector<int> fndIndex;

int h = 1;
for (int i = 0; i < m - 1; i++)
{
h = (h * d) % q;
}

int p = 0;
int t = 0;

for (int i = 0; i < m; i++)
{
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}

for (int i = 0; i <= n - m; i++)
{
if (p == t)
{
int j;
for (j = 0; j < m; j++)
{
if (text[i + j] != pattern[j])
{
break;
}
}

if (j == m)
{
fndIndex.push_back(i);
cout << "Pattern found at index " << i << endl;
}
}

if (i < n - m)
{
t = (d * (t - text[i] * h) + text[i + m]) % q;
if (t < 0)
{
t += q;
}
}
}

return fndIndex;
}

int main()
{
string text = "ababcababcabab";
string pattern = "abc";

vector<int> fndIndex = robin_karp(text, pattern);

if (fndIndex.size() == 0)
{
cout << "Pattern not found" << endl;
}

return 0;
}

0 comments on commit 6cf1c8f

Please sign in to comment.