mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
style: enable ConstantName
in checkstyle (#5139)
Co-authored-by: Maria Paszkiewicz SCC <maria.paszkiewicz@kit.edu>
This commit is contained in:
@ -6,8 +6,7 @@ public final class RabinKarpAlgorithm {
|
||||
private RabinKarpAlgorithm() {
|
||||
}
|
||||
|
||||
// d is the number of characters in the input alphabet
|
||||
private static final int d = 256;
|
||||
private static final int ALPHABET_SIZE = 256;
|
||||
|
||||
public static int search(String pattern, String text, int primeNumber) {
|
||||
|
||||
@ -19,13 +18,13 @@ public final class RabinKarpAlgorithm {
|
||||
int h = 1;
|
||||
|
||||
// The value of h would be "pow(d, patternLength-1)%primeNumber"
|
||||
for (int i = 0; i < patternLength - 1; i++) h = (h * d) % primeNumber;
|
||||
for (int i = 0; i < patternLength - 1; i++) h = (h * ALPHABET_SIZE) % primeNumber;
|
||||
|
||||
// Calculate the hash value of pattern and first
|
||||
// window of text
|
||||
for (int i = 0; i < patternLength; i++) {
|
||||
hashForPattern = (d * hashForPattern + pattern.charAt(i)) % primeNumber;
|
||||
hashForText = (d * hashForText + text.charAt(i)) % primeNumber;
|
||||
hashForPattern = (ALPHABET_SIZE * hashForPattern + pattern.charAt(i)) % primeNumber;
|
||||
hashForText = (ALPHABET_SIZE * hashForText + text.charAt(i)) % primeNumber;
|
||||
}
|
||||
|
||||
// Slide the pattern over text one by one
|
||||
@ -51,7 +50,7 @@ public final class RabinKarpAlgorithm {
|
||||
// Calculate hash value for next window of text: Remove
|
||||
// leading digit, add trailing digit
|
||||
if (i < textLength - patternLength) {
|
||||
hashForText = (d * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
|
||||
hashForText = (ALPHABET_SIZE * (hashForText - text.charAt(i) * h) + text.charAt(i + patternLength)) % primeNumber;
|
||||
|
||||
// handling negative hashForText
|
||||
if (hashForText < 0) hashForText = (hashForText + primeNumber);
|
||||
|
Reference in New Issue
Block a user