style: enable ConstantName in checkstyle (#5139)

Co-authored-by: Maria Paszkiewicz SCC <maria.paszkiewicz@kit.edu>
This commit is contained in:
marysiuniq
2024-05-02 18:31:37 +02:00
committed by GitHub
parent ede3e4651f
commit 1e2d7e9431
16 changed files with 87 additions and 90 deletions

View File

@ -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);