Questions tagged [rabin-karp]

The Rabin-Karp string matching algorithm is a string matching algorithm that employs a rolling hash function to speed up the search.

Wiki

In computer science, the Rabin–Karp algorithm is a string searching algorithm. For a text of length n and p patterns of combined length m, its average and best case running time is O(n+m) in space O(p), but its worst-case time is O(nm).

Algorithm pseudocode

function RabinKarp(string s[1..n], string sub[1..m])
  hsub := hash(sub[1..m]);  hs := hash(s[1..m])        
  for i from 1 to n-m+1
    if hs = hsub
      if s[i..i+m-1] = sub
        return i
    hs := hash(s[i+1..i+m])
  return not found

Tag usage

The tag can be used for programming related problems in implementing Rabin-Karp algorithm in any programming language. Please avoid theoretical and conceptual questions on StackOverflow using the tag .

Read more

107 questions
2
votes
1 answer

Hashing n-grams by cyclic polynomials - java implementation

I'm solving some problem that involves Rabin–Karp string search algorithm. This algorithm requires rolling hash to be faster then naive search. This article describes how to implement rolling hash. I implemented "Rabin-Karp rolling hash" without…
Jeriho
  • 7,129
  • 9
  • 41
  • 57
2
votes
1 answer

Find longest palindromic substring using Rabin-Karp algorithm

From https://algs4.cs.princeton.edu/53substring/ 15. Longest palindromic substring. Given a string s, find the longest substring that is a palindrome (or a Watson-crick palindrome). Solution: can be solved in linear time using suffix trees…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
2
votes
0 answers

Extending Rabin-Karp algorithm to hash a 2D matrix

I'm trying to solve a problem here, it asks to find the size of the biggest common subsquare between two matrices. e.g. Matrix #1 3 3 1 2 0 1 2 1 1 2 3 Matrix #2 3 3 0 1 2 1 1 2 3 1 2 Answer: 2 Biggest common subsquare is: 1 2 1 2 I know that…
2
votes
1 answer

How to prevent this Cyclic polynomial hash function from using a type constraint?

I am trying to implement the Cyclic polynomial hash function in f#. It uses the bit-wise operators ^^^ and <<<. Here is an example of a function that hashes an array: let createBuzhash (pattern : array<'a>) = let n = pattern.Length let rec loop…
kMutagene
  • 177
  • 10
2
votes
1 answer

Rabin Karp algorithm for big strings

I wrote a simple step-by-step implementation of Rabin-Karp algorithm for substring search, and it seems to work fine until the hash becomes greater than the modulus, and then it goes wrong... Here is the code, it's quite simple: typedef long long…
Daniel
  • 7,357
  • 7
  • 32
  • 84
2
votes
1 answer

What does "h" represent in rabin karp algorithm?

t(s+1) = (d*(t(s) -T[s+1]h) + T[s+m+1])mod q d is the size of alphabet T[1...n] is the text to be searched P[1...m] is the pattern (m is the size of pattern) q is a prime number h = d^m-1 (mod q) is the value of digit "1" in the higher order…
naman jain
  • 23
  • 2
2
votes
0 answers

Using Mod in Rabin Karp Algorithm

I am learning about substring algorithms, esp Rabin Karp substring matching method from here and online sources. I see that for comparing longer substrings, we generally take the modulus. What are the characteristics of this K for the…
chipmunk
  • 545
  • 2
  • 9
  • 18
2
votes
1 answer

Rabin-Karp rolling hash

In one of the Coursera videos the Rabin-Karp rolling hash (http://en.wikipedia.org/wiki/Rolling_hash) is shown as: public static long getHash(String S) { long H = 0; for (int i = 0; i < S.length(); i++) H = (H * 10 + S.charAt(i)) %…
good_evening
  • 21,085
  • 65
  • 193
  • 298
2
votes
1 answer

Rolling Hash Algorithm for Rabin-Karp Algorithm Java

I have been trying to understand the Rabin-Karp algorithm for an algorithms class. I was having alot of trouble understanding it so I tried to implement it (I don't actually have to implement it). I think I properly understand everything except the…
MichelleJS
  • 759
  • 3
  • 16
  • 32
2
votes
1 answer

longest common sub-string, Python complexity analysis

I built a function which finds the longest common sub-string of two text files in ascending order based on Rabin–Karp algorithm. the main function is "find_longest" and the inner functions are: "make_hashtable","extend_fingerprints" and…
GroundIns
  • 541
  • 1
  • 5
  • 11
2
votes
1 answer

Finding a good hash function for Rabin-Karp string search algorithm

What are some good hash functions that can be used for implementing Rabin-Karp string search algorithm? I only know of polynomial hash, but it has some flaws — most notably, if hashing is done modulo 264, there is a test which is guaranteed to…
Emily
  • 2,577
  • 18
  • 38
1
vote
0 answers

Rabin Karp implementation the precomputed hash values and Hash function values don't match

I have Rabin Karp implementation in C++ (Rabin Karp is a string pattern matching algorithm that uses hashing technique to match substrings [Wiki link to the algorithm] (https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)), A trivial test…
Pawan Nirpal
  • 565
  • 1
  • 12
  • 29
1
vote
0 answers

Understanding Rabin-Karp algorithm with modulo arithmetic

I am having a hard time understanding the Rabin-Karp algorithm with modulo arithmetic. My questions. Why we use modulus to determine the hash of the string being analyzed? How to determine the modulus to be used?
1
vote
1 answer

'highlighting' a string according to a given pattern

I have a bunch of strings such as 1245046126856123 5293812332348977 1552724141123171 7992612370048696 6912394320472896 I give the program a pattern which in this case is '123' and the expected output…
STT
  • 59
  • 6
1
vote
1 answer

Rabin-Karp algorithm in c++

I am trying to understand the implementation of the Rabin-Karp algorithm. d is the number of characters in the input alphabet, but if I replace 0 or any other value instead of 20, it won't affect anything. Why is this happening like this ? //…
Kris
  • 13
  • 3