Manacher's Algorithm is an approach to find the longest palindromic substring in a string. It works by preprocessing the string and using the concept of palindrome symmetry to reduce comparisons. The algorithm inserts special characters between characters, uses an array to store palindrome lengths centered at each position, and expands palindromes to find the longest one.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views
L30 - Manacher's Algorithm
Manacher's Algorithm is an approach to find the longest palindromic substring in a string. It works by preprocessing the string and using the concept of palindrome symmetry to reduce comparisons. The algorithm inserts special characters between characters, uses an array to store palindrome lengths centered at each position, and expands palindromes to find the longest one.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
MANACHER’S ALGORITHM
WHAT IS MANACHER’S ALGORITHM?
• Manacher's Algorithm is a well-known approach for determining the
longest palindromic substring within a given string.
• The Algorithm uses the concept of palindrome symmetry to reduce the
number of comparisons required to find the longest palindromic substring.
• Manacher's Algorithm is a highly effective method for identifying the
longest palindromic substring within a given string. EXAMPLE: • Suppose we have the string "abaxabaxabb". We can create a new string by inserting a "#" character between each pair of characters: "a#b#a#x#a#b#a#x#a#b#b#". Then we can apply Manacher's Algorithm as follows: • Let c be the center of the longest length palindrome we have encountered till now. And let l and r be the left and right boundaries of this palindrome, i.e., the left-most character index and the right-most character index respectively. Now, let’s take an example to understand c, l, and r. • Eg: “abacabacabb” • When going from left to right, when i is at index 1, the longest palindromic substring is “aba” (length = 3). c, l, and r for palindromic string “aba”
The answer for the given string is 9 when the palindrome is centered at index 5; c, l, and r are as follows:
Final c, l, and r positions for the whole string
ALGORITHM 1. Create a new string by inserting a special character (such as #) between each pair of characters in the original string. It ensures that every palindrome in the new string has an odd length. 2. Initialize two variables: Center and right. The Center represents the Center of the palindrome we have found so far, and the right represents the rightmost boundary of this palindrome. 3. Initialize an array p to store the length of the palindrome centered at each position in the new string. Initially, set all elements to zero. 4. Loop through each position in the new string. At each position i, do the following: a. If i is less than right, set p[i] to the minimum of p[2 * center - i] and right - i. We can reuse information from previously discovered palindromes to avoid redundant work. b. Expand the palindrome centered at i as far as possible, updating p[i] accordingly. c. If the right boundary of this palindrome extends beyond the current value of the right, update the Center and right accordingly. 5. Find the index of the maximum value in p. It represents the Center of the longest palindrome in the new string. 6. Use the Center and the palindrome length to extract the corresponding substring from the original string. 1.Initialize center = 0 and right = 0. 2.Initialize p to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. 3.Loop through each position i in the new string: 1. At i = 0, p[0] = 0. 2. At i = 1, p[1] = 1, center = 1, right = 2. 3. At i = 2, p[2] = 0. 4. At i = 3, p[3] = 3, center = 3, right = 6. 5. At i = 4, p[4] = 0. 6. At i = 5, p[5] = 1, center = 3, right = 6. 7. At i = 6, p[6] = 0. 8. At i = 7, p[7] = 1, center = 7, right = 8. 9. At i = 8, p[8] = 0. 10. At i = 9, p[9] = 1, center = 7, right = 10. 11. At i = 10, p[10] = 2, center = 10, right = 12. 12. At i = 11, p[11] = 0. 13. At i = 12, p[12] = 1, center = 10, right = 14. 14. At i = 13, p[13] = 0. 15. At i = 14, p[14] = 1, center = 14, right = 16. 16. At i = 15, p[15] = 0. 17. At i = 16, p[16] = 1, center = 14, right = 18. 18. At i = 17, p[17] = 0. 19. At i = 18, p[18] = 0. 20. At i = 19, p[19] = 0. 21. At i = 20, p[20] = 0. 22. At i = 21, p[21] = 0. 23. At i = 22, p[22] = 0. 24. At i = 23, p[23] = 0. 25. At i = 24, p[24] = 0. 26. At i = 25, p[25] = 0. Find the index of the maximum value in p, which is 14. The longest palindromic substring in the new string is "a#b#a#x#a#b#a#x#a#b#a", which corresponds to the original string "abaxabaxaba". Since the "#" characters were only added for the algorithm, we can remove them and get the original palindrome. public class ManachersAlgorithm { public static String longestPalindromicSubstring(String str) { // Step 1: Modify the input string StringBuilder sb = new StringBuilder(); sb.append("#"); // Add a special character '#' at the beginning of the st ring for (int i = 0; i < str.length(); i++) { sb.append(str.charAt(i)); sb.append("#"); // Add '#' between each pair of characters } String s = sb.toString(); // Modified string // Step 2: Apply Manacher's Algorithm int[] p = new int[s.length()]; // Array to store the palindrome lengths int center = 0, right = 0; // Center and rightmost boundary of the palindrome for (int i = 1; i < s.length() - 1; i++) { // Calculate the mirror index of i int mirror = 2 * center - i; // If i is within the boundary of the palindrome, use the mirror value of i if (i < right) { p[i] = Math.min(right - i, p[mirror]); } // Expand the palindrome around i while (i + p[i] + 1 < s.length() && i - p[i] - 1 >= 0 && s.charAt(i + p[i] + 1) == s.ch arAt(i - p[i] - 1)) { p[i]++; } // Update the Center and right boundary if the palindrome expands beyond the current boundary if (i + p[i] > right) { center = i; right = i + p[i]; } }
// Step 3: Find the longest palindromic substring
int maxLen = 0, centerIndex = 0; for (int i = 1; i < p.length - 1; i++) { if (p[i] > maxLen) { maxLen = p[i]; centerIndex = i; } } // Calculate the start index of the longest palindrome in the original string int startIndex = (centerIndex - maxLen) / 2; return str.substring(startIndex, startIndex + maxLen); // Return the longest pa lindrome }