mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
style: enable LocalVariableName
in CheckStyle (#5191)
* style: enable LocalVariableName in checkstyle * Removed minor bug * Resolved Method Name Bug * Changed names according to suggestions
This commit is contained in:
@ -3,25 +3,25 @@ package com.thealgorithms.searches;
|
||||
class KMPSearch {
|
||||
|
||||
int kmpSearch(String pat, String txt) {
|
||||
int M = pat.length();
|
||||
int N = txt.length();
|
||||
int m = pat.length();
|
||||
int n = txt.length();
|
||||
|
||||
// create lps[] that will hold the longest
|
||||
// prefix suffix values for pattern
|
||||
int[] lps = new int[M];
|
||||
int[] lps = new int[m];
|
||||
int j = 0; // index for pat[]
|
||||
|
||||
// Preprocess the pattern (calculate lps[]
|
||||
// array)
|
||||
computeLPSArray(pat, M, lps);
|
||||
computeLPSArray(pat, m, lps);
|
||||
|
||||
int i = 0; // index for txt[]
|
||||
while ((N - i) >= (M - j)) {
|
||||
while ((n - i) >= (m - j)) {
|
||||
if (pat.charAt(j) == txt.charAt(i)) {
|
||||
j++;
|
||||
i++;
|
||||
}
|
||||
if (j == M) {
|
||||
if (j == m) {
|
||||
System.out.println("Found pattern "
|
||||
+ "at index " + (i - j));
|
||||
int index = (i - j);
|
||||
@ -29,7 +29,7 @@ class KMPSearch {
|
||||
return index;
|
||||
}
|
||||
// mismatch after j matches
|
||||
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
|
||||
else if (i < n && pat.charAt(j) != txt.charAt(i)) {
|
||||
// Do not match lps[0..lps[j-1]] characters,
|
||||
// they will match anyway
|
||||
if (j != 0)
|
||||
@ -42,14 +42,14 @@ class KMPSearch {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void computeLPSArray(String pat, int M, int[] lps) {
|
||||
void computeLPSArray(String pat, int m, int[] lps) {
|
||||
// length of the previous longest prefix suffix
|
||||
int len = 0;
|
||||
int i = 1;
|
||||
lps[0] = 0; // lps[0] is always 0
|
||||
|
||||
// the loop calculates lps[i] for i = 1 to M-1
|
||||
while (i < M) {
|
||||
// the loop calculates lps[i] for i = 1 to m-1
|
||||
while (i < m) {
|
||||
if (pat.charAt(i) == pat.charAt(len)) {
|
||||
len++;
|
||||
lps[i] = len;
|
||||
|
@ -18,7 +18,7 @@ public final class OrderAgnosticBinarySearch {
|
||||
static int binSearchAlgo(int[] arr, int start, int end, int target) {
|
||||
|
||||
// Checking whether the given array is ascending order
|
||||
boolean AscOrd = arr[start] < arr[end];
|
||||
boolean ascOrd = arr[start] < arr[end];
|
||||
|
||||
while (start <= end) {
|
||||
int middle = start + (end - start) / 2;
|
||||
@ -27,7 +27,7 @@ public final class OrderAgnosticBinarySearch {
|
||||
if (arr[middle] == target) return middle; // returns the index of the middle element
|
||||
|
||||
// Ascending order
|
||||
if (AscOrd) {
|
||||
if (ascOrd) {
|
||||
if (arr[middle] < target)
|
||||
start = middle + 1;
|
||||
else
|
||||
|
Reference in New Issue
Block a user