Given a string, the task is to return the length of its longest possible chunked palindrome. It means a palindrome formed by substring in the case when it is not formed by characters of the string. For a better understanding look at the example
Input : ghiabcdefhelloadamhelloabcdefghi Output : 7 (ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi) Input : merchant Output : 1 (merchant) Input : antaprezatepzapreanta Output : 11 (a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a) Input : geeksforgeeks Output : 3 (geeks)(for)(geeks)
The entire idea is to create chunks from left and right and recursively.
As you can see, we can match substring from the left side chunk and match it with the exact right side chunk. Once we get a match, we recursively count the length of the longest possible chunked palindrome in the remaining string. We end the recursion once no string is left or when no more valid chunked parts can be found.
V : 1 VOLVO : 3 VOLVOV : 5 ghiabcdefhelloadamhelloabcdefghi : 7 ghiabcdefhelloadamhelloabcdefghik : 1 antaprezatepzapreanta : 11
Following is the C++ implementation with memoization for above problem.
This article is contributed by Aditya Nihal Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Find longest palindrome formed by removing or shuffling chars from string
- Length of longest palindrome list in a linked list using O(1) extra space
- Longest palindrome subsequence with O(n) space
- Print longest palindrome word in a sentence
- Longest double string from a Palindrome
- Length of the longest substring that do not contain any palindrome
- Longest palindrome formed by concatenating and reordering strings of equal length
- Longest Palindrome in a String formed by concatenating its prefix and suffix
- Check if longest connected component forms a palindrome in undirected graph
- Longest substring whose characters can be rearranged to form a Palindrome
- Length of Longest Palindrome Substring
- Rearrange characters to form palindrome if possible
- Check if it is possible to create a palindrome string from given N
- Longest palindromic string possible after removal of a substring
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Function to check if a singly linked list is palindrome
- Palindrome Partitioning | DP-17
- Given a number, find the next smallest palindrome