Skip to content

Commit

Permalink
[C++] Rabin-Karp Algorithm (String Matching) 🔡
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmehara committed Mar 16, 2024
1 parent b5c38a5 commit 9bb2114
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions C++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ add_subdirectory(mathematics)
add_subdirectory(randomized_algorithms)
add_subdirectory(search)
add_subdirectory(sort)
add_subdirectory(string_algorithms)
add_subdirectory(test)
3 changes: 3 additions & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@
* Merge Sort
* Quick Sort
* Selection Sort

## String Algorithms
* Rabin-Karp Algorithm
4 changes: 4 additions & 0 deletions C++/string_algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target_sources(programming
PRIVATE
rabin_karp_matcher.h
rabin_karp_matcher.cpp)
62 changes: 62 additions & 0 deletions C++/string_algorithms/rabin_karp_matcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Aman Mehara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "rabin_karp_matcher.h"
#include "../mathematics/number_theory/exponentiation.h"

#include <cmath>
#include <string>
#include <vector>

namespace mehara::string {

std::vector<int> rabin_karp_matcher(const std::string& text,
const std::string& pattern, int radix,
int prime)
{
std::vector<int> matches;
int n = text.size();
int m = pattern.size();
int h = mathematics::modular_exponentiation(radix, m - 1, prime);
int pattern_hash = 0;
int rolling_hash = 0;
for (int i = 0; i < m; i++) {
pattern_hash = (radix * pattern_hash + pattern[i]) % prime;
rolling_hash = (radix * rolling_hash + text[i]) % prime;
}
for (int i = 0; i <= n - m; i++) {
if (pattern_hash == rolling_hash) {
bool match = true;
for (int j = 0; j < m; j++) {
if (pattern[j] != text[i + j]) {
match = false;
break;
}
}
if (match) {
matches.push_back(i);
}
}
if (i < n - m) {
rolling_hash =
(radix * (rolling_hash - text[i] * h) + text[i + m]) % prime;
if (rolling_hash < 0) {
rolling_hash += prime;
}
}
}
return matches;
}

} // namespace mehara::string
29 changes: 29 additions & 0 deletions C++/string_algorithms/rabin_karp_matcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 Aman Mehara
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MEHARA_RABIN_KARP_H_
#define MEHARA_RABIN_KARP_H_

#include <string>
#include <vector>

namespace mehara::string {

std::vector<int> rabin_karp_matcher(const std::string& text,
const std::string& pattern, int radix,
int prime);

} // namespace mehara::string

#endif // MEHARA_RABIN_KARP_H_

0 comments on commit 9bb2114

Please sign in to comment.