Question: Please help with this method /** * Prime base used for Rabin-Karp hashing. * DO NOT EDIT! */ private static final int BASE = 101;

Please help with this method

/** * Prime base used for Rabin-Karp hashing. * DO NOT EDIT! */ private static final int BASE = 101;

/** * Runs the Rabin-Karp algorithm. This algorithms generates hashes for the * pattern and compares this hash to substrings of the text before doing * character by character comparisons. * * When the hashes are equal and you do character comparisons, compare * starting from the beginning of the pattern to the end, not from the end * to the beginning. * * You must use the Rabin-Karp Rolling Hash for this implementation. The * formula for it is: * * sum of: c * BASE ^ (pattern.length - 1 - i), where c is the integer * value of the current character, and i is the index of the character * * Note that if you were dealing with very large numbers here, your hash * will likely overflow. However, you will not need to handle this case. * You may assume there will be no overflow. * * For example: Hashing "bunn" as a substring of "bunny" with base 101 hash * = b * 101 ^ 3 + u * 101 ^ 2 + n * 101 ^ 1 + n * 101 ^ 0 = 98 * 101 ^ 3 + * 117 * 101 ^ 2 + 110 * 101 ^ 1 + 110 * 101 ^ 0 = 102174235 * * Another key step for this algorithm is that updating the hashcode from * one substring to the next one must be O(1). To update the hash: * * remove the oldChar times BASE raised to the length - 1, multiply by * BASE, and add the newChar. * * For example: Shifting from "bunn" to "unny" in "bunny" with base 101 * hash("unny") = (hash("bunn") - b * 101 ^ 3) * 101 + y = * (102174235 - 98 * 101 ^ 3) * 101 + 121 = 121678558 * * Keep in mind that calculating exponents is not O(1) in general, so you'll * need to keep track of what BASE^{m - 1} is for updating the hash. * * Do NOT use Math.pow * * @throws IllegalArgumentException if the pattern is null or of length 0 * @throws IllegalArgumentException if text or comparator is null * @param pattern a string you're searching for in a body of text * @param text the body of text where you search for pattern * @param comparator the comparator to use when checking character equality * @return list containing the starting index for each match found */ public static List rabinKarp(CharSequence pattern, CharSequence text, CharacterComparator comparator) {

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!