Loading topic
text of length n and a shorter string pattern of length m, at which positions does pattern occur in text? Editors, grep, DNA sequence tools, plagiarism checkers, and log scanners all answer this question constantly. This chapter 6 min read% does on non-negative integers, and have read the previous chapter on modular arithmetic. In particular, it relies on the fact that you can reduce modulo mod after every addition and multiplication without changing the final remainde6 min readm, it produ6 min readh(s) = (s[0]·b^(m-1) + ... + s[m-1]) mod p and slide it one character in O(1). You also need basic Python and the modulo operator.6 min readrabin_karp function that confirms every hash match with a direct comparison. Throughout, n is the text length, m is the pattern length, and p i6 min readh = (h * base + ord(ch)) % mod, the rolling update, and why Rabin-Karp verifies every hash match. Here we look at the two numbers that hash depends on. The base and the modulus decide how often collisions happen, how fast the arithmetic run5 min read% always returns a result with the same sign as the divisor. Java and C++ give you neither guarantee. This chapter covers the two bugs that appear when6 min reads[3:9] equal to s[17:23]?" for arbitrary positions, of6 min read&, ^, <<, >>) in Python. The rolling hash is u6 min read