@@ -29,9 +29,13 @@ public static String longestPalindrome(String s) {
29
29
// Preprocess the string to avoid even-length palindrome issues
30
30
String processedString = preprocess (s );
31
31
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 ;
35
39
36
40
// Iterate over the preprocessed string to calculate the palindrome radii
37
41
for (int i = 1 ; i < n - 1 ; i ++) {
@@ -40,23 +44,23 @@ public static String longestPalindrome(String s) {
40
44
41
45
// If the current index is within the right boundary, mirror the palindrome radius
42
46
if (i < rightBoundary ) {
43
- P [i ] = Math .min (rightBoundary - i , P [mirror ]);
47
+ p [i ] = Math .min (rightBoundary - i , p [mirror ]);
44
48
}
45
49
46
50
// 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 ]++;
49
53
}
50
54
51
55
// Update center and right boundary if palindrome expands beyond current right boundary
52
- if (i + P [i ] > rightBoundary ) {
56
+ if (i + p [i ] > rightBoundary ) {
53
57
center = i ;
54
- rightBoundary = i + P [i ];
58
+ rightBoundary = i + p [i ];
55
59
}
56
60
57
61
// 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 ];
60
64
centerIndex = i ;
61
65
}
62
66
}
0 commit comments