Skip to content

Commit 4c11045

Browse files
committed
Formatted Manacher.java
1 parent b4ae4aa commit 4c11045

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/main/java/com/thealgorithms/strings/Manacher.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ public static String longestPalindrome(String s) {
2929
// Preprocess the string to avoid even-length palindrome issues
3030
String processedString = preprocess(s);
3131
int n = processedString.length();
32-
int[] P = new int[n]; // Array to store the radius of palindromes
33-
int center = 0, rightBoundary = 0; // Current center and right boundary of the palindrome
34-
int maxLen = 0, centerIndex = 0; // To track the longest palindrome
32+
int[] p = new int[n]; // Array to store the radius of palindromes
33+
34+
// Separate variable declarations into individual statements
35+
int center = 0;
36+
int rightBoundary = 0;
37+
int maxLen = 0;
38+
int centerIndex = 0;
3539

3640
// Iterate over the preprocessed string to calculate the palindrome radii
3741
for (int i = 1; i < n - 1; i++) {
@@ -40,23 +44,23 @@ public static String longestPalindrome(String s) {
4044

4145
// If the current index is within the right boundary, mirror the palindrome radius
4246
if (i < rightBoundary) {
43-
P[i] = Math.min(rightBoundary - i, P[mirror]);
47+
p[i] = Math.min(rightBoundary - i, p[mirror]);
4448
}
4549

4650
// Try to expand the palindrome centered at i
47-
while (processedString.charAt(i + 1 + P[i]) == processedString.charAt(i - 1 - P[i])) {
48-
P[i]++;
51+
while (processedString.charAt(i + 1 + p[i]) == processedString.charAt(i - 1 - p[i])) {
52+
p[i]++;
4953
}
5054

5155
// Update center and right boundary if palindrome expands beyond current right boundary
52-
if (i + P[i] > rightBoundary) {
56+
if (i + p[i] > rightBoundary) {
5357
center = i;
54-
rightBoundary = i + P[i];
58+
rightBoundary = i + p[i];
5559
}
5660

5761
// Track the maximum length and center index of the longest palindrome found so far
58-
if (P[i] > maxLen) {
59-
maxLen = P[i];
62+
if (p[i] > maxLen) {
63+
maxLen = p[i];
6064
centerIndex = i;
6165
}
6266
}

0 commit comments

Comments
 (0)