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
0
votes
1 answer

c- Karp-Rabin rolling hash - skip and append parts

I need a little help with a particular part of my Karp-Rabin algo. What I am trying to do is to implement both the version with fixed sliding window and with separate append and skip parts. Sliding window works perfectly fine. The problem occurs…
Anton Tretyakov
  • 303
  • 1
  • 9
0
votes
1 answer

How to assign numbers to letters for anagram search using modified Rabin-Karp

I have to assign numbers to letters a,b,c,d...z such that for a given string and all anagram search we can do it in o(n) using hash search. The hash function is supposedly s[0]+s[1]+s[2]..s[n-1]. Anagram is positional independent so there is no need…
MAG
  • 2,841
  • 6
  • 27
  • 47
0
votes
0 answers

Choosing Base and Mod prime numbers in Rabin Karp rolling hash function

This has been asked a lot but I have a scenario where what if the vector size is already known and there is no repetition in the characters or integers ? Suppose: vector v = { 1,2,3,4,5,6,7,8,9,10}; We already know that I will have a…
Coder
  • 64
  • 6
0
votes
1 answer

Leetcode 1044. Longest Duplicate Substring (small question in terms of modulus)

I was solving Leetcode 1044 and the answer is using binary search and rolling hash. Basically use binary search to select a length and then do a search for duplicate string of that length. Here rolling hash comes into play to save space (instead of…
0
votes
2 answers

Is rabin-karp string search algorithm still correct if we neglect the modulo part and let hash int/long overflow?

I have a question: if we let rolling hash overflow, does it affect the correctness of Rabin-Karp algorithm? Could you give a solid example that the overflow indeed will affect correctness? That is something like same string e.g. "abcd" will give…
maplemaple
  • 1,297
  • 6
  • 24
0
votes
1 answer

Go: polynomial fingerprint for string comparison

I want to implement a rolling hash function to do string comparison (Rabin-Karp) To do so, I convert my input string into a slice of bytes (using go unicode/utf8) and operate the "polynomial fingerprinting" function on it. For instance, I input the…
Kuruwan
  • 54
  • 1
  • 8
0
votes
1 answer

Rabin Karp algorithm runs slower than naive

I implemented the Rabin-Karp algorithm but it runs slower than the naive string search (this is not for an assignment, I'm just coding it to understand it better). Rabin Karp (I'm using 128 as the base for ascii): public long honer(String s){ …
qwerty_99
  • 640
  • 5
  • 20
0
votes
1 answer

C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value

i am trying to write a program that searches through a movie script using two different string searching algorithms. However the Warning C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value keeps…
binlardas
  • 3
  • 3
0
votes
1 answer

Why are two different hashes being generated for the same subStrings and what can I do to solve this?

I wrote the following code to try a simplistic implementation of the Rabin-Karp algorithm. public int charToInt(int index, String str){ return (int)str.charAt(index); } public int strStr(String haystack, String needle) { …
anmol
  • 1
0
votes
0 answers

Robin carp implementation does not give the desired result

The below code is not giving the desired output after using 1000000007 to hash. Using 101 to hash make it work desirably. Can you help me in finding the where I am getting wrong? #include using namespace std; typedef vector< int >…
0
votes
1 answer

Explanation to Rabin Karp from CLRS

I've been reading the Rabin Karp algorithm from Introduction To Algorithms. Everything makes sense except the following. In general, with a d-ary alphabet {0, 1, . . ., d - 1}, we choose q so that dq fits within a computer word I don't understand…
Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63
0
votes
1 answer

Karp-Rabin algorithm

The below image is from : 6.006-Introduction to algorithms, While doing the course 6.006-Introduction to algorithms, provided by MIT OCW, I came across the Rabin-Karp algorithm. Can anyone help me understand as to why the first rs()==rt()…
user22430
  • 75
  • 7
0
votes
1 answer

How to incorporate mod in rolling hash of Rabin Karp algorithm?

I am trying to implement the Rabin Karp algorithm with mod. The hash function which i am using is: H1= c1*a^k-1 + c2*a^k-2 +c3*a^k-3 +…+ck*a^0 Here cx is the ASCII value of the character. And to roll it I first drop the first term by subtracting…
user6575289
0
votes
1 answer

Pattern search using Rabin Karp

I am working on Rabin Karp algorithm using recurrence formula. Following is code. In code I am checking hash value calculated normal way and with recurrence formula. Both values are not matching. I spent enough time almost 3 hours debugging, not…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
0
votes
3 answers

How do you detect a pattern match when comparing two unique strings?

I'm looking for a solution to the following string pattern matching problem. You've got a function that takes two arguments: pattern, and input - both are strings. Let's say pattern: aabbaa and input: catcatdogdogcatcat These specific arguments…
zero_cool
  • 3,960
  • 5
  • 39
  • 54