From 6cf1c8f507ef0a7691700ddfbc7454a628d1bcaa Mon Sep 17 00:00:00 2001 From: Mahmud-Araf Date: Tue, 1 Oct 2024 00:03:44 +0600 Subject: [PATCH] String matching created --- .gitignore | 1 + Special/inversion_counting.cpp | 1 + String Matching/kmp_approach.cpp | 92 +++++++++++++++++++++++++ String Matching/naive_approach.cpp | 50 ++++++++++++++ String Matching/robin_karp_approach.cpp | 79 +++++++++++++++++++++ 5 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 String Matching/kmp_approach.cpp create mode 100644 String Matching/naive_approach.cpp create mode 100644 String Matching/robin_karp_approach.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a3e68d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/.DS_Store \ No newline at end of file diff --git a/Special/inversion_counting.cpp b/Special/inversion_counting.cpp index 0f216d3..97dba7e 100644 --- a/Special/inversion_counting.cpp +++ b/Special/inversion_counting.cpp @@ -1,4 +1,5 @@ // implementation of invertion counting using merge sort + #include using namespace std; diff --git a/String Matching/kmp_approach.cpp b/String Matching/kmp_approach.cpp new file mode 100644 index 0000000..60af952 --- /dev/null +++ b/String Matching/kmp_approach.cpp @@ -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 +using namespace std; + + +void computeLPS(string pattern, vector& 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++; + } + } + } +} + +vectorkmp_approach(string text, string pattern) +{ + int n = text.length(); + int m = pattern.length(); + vector fndIndex; + vector 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 fndIndex = kmp_approach(text, pattern); + if (fndIndex.size() == 0) + { + cout << "Pattern not found" << endl; + } + return 0; +} \ No newline at end of file diff --git a/String Matching/naive_approach.cpp b/String Matching/naive_approach.cpp new file mode 100644 index 0000000..ff2310c --- /dev/null +++ b/String Matching/naive_approach.cpp @@ -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 +using namespace std; + + +vector naive_approach(string text, string pattern) +{ + int n = text.length(); + int m = pattern.length(); + vector 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 fndIndex = naive_approach(text, pattern); + + if (fndIndex.size() == 0) + { + cout << "Pattern not found" << endl; + } + + return 0; +} \ No newline at end of file diff --git a/String Matching/robin_karp_approach.cpp b/String Matching/robin_karp_approach.cpp new file mode 100644 index 0000000..2334bd3 --- /dev/null +++ b/String Matching/robin_karp_approach.cpp @@ -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 +using namespace std; + +#define d 256 +#define q INT_MAX + +vector robin_karp(string text, string pattern) +{ + int n = text.length(); + int m = pattern.length(); + vector 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 fndIndex = robin_karp(text, pattern); + + if (fndIndex.size() == 0) + { + cout << "Pattern not found" << endl; + } + + return 0; +} \ No newline at end of file