Longest palindromic string possible after removal of a substring Last Updated : 13 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given a string str, the task is to find the longest palindromic string that can be obtained from it after removing a substring. Examples: Input: str = "abcdefghiedcba" Output: "abcdeiedcba" Explanation: Removal of substring "fgh" leaves the remaining string palindromic Input: str = "abba" Output: "abba" Explanation: Removal of substring "" as the given string is already palindromic. Approach: Find the longest possible pair of substrings A and B from both ends of the given string which are reverse of each other.Remove them from the original string.Find the longest palindromic substrings from both ends of the remaining string using KMP and consider the substring which is longer.Add the strings A and B to beginning and end of this palindromic substring respectively to get the desired output. Below code is the implementation of the above approach: C++ // C++ Implementation of the // above approach #include <bits/stdc++.h> using namespace std; // Function to find the longest palindrome // from the start of the string using KMP match string findPalindrome(string C) { string S = C; reverse(S.begin(), S.end()); // Append S(reverse of C) to C C = C + "&" + S; int n = C.length(); int longestPalindrome[n]; longestPalindrome[0] = 0; int len = 0; int i = 1; // Use KMP algorithm while (i < n) { if (C[i] == C[len]) { len++; longestPalindrome[i] = len; i++; } else { if (len != 0) { len = longestPalindrome[len - 1]; } else { longestPalindrome[i] = 0; i++; } } } string ans = C.substr(0, longestPalindrome[n - 1]); return ans; } // Function to return longest palindromic // string possible from the given string // after removal of any substring string findAns(string s) { // Initialize three strings A, B AND F string A = ""; string B = ""; string F = ""; int i = 0; int j = s.length() - 1; int len = s.length(); // Loop to find longest substrings // from both ends which are // reverse of each other while (i < j && s[i] == s[j]) { i = i + 1; j = j - 1; } if (i > 0) { A = s.substr(0, i); B = s.substr(len - i, i); } // Proceed to third step of our approach if (len > 2 * i) { // Remove the substrings A and B string C = s.substr(i, s.length() - 2 * i); // Find the longest palindromic // substring from beginning of C string D = findPalindrome(C); // Find the longest palindromic // substring from end of C reverse(C.begin(), C.end()); string E = findPalindrome(C); // Store the maximum of D and E in F if (D.length() > E.length()) { F = D; } else { F = E; } } // Find the final answer string answer = A + F + B; return answer; } // Driver Code int main() { string str = "abcdefghiedcba"; cout << findAns(str) << endl; } Java // Java Implementation of the // above approach import java.util.*; class GFG{ // Function to find the longest palindrome // from the start of the String using KMP match static String findPalindrome(String C) { String S = C; S = reverse(S); // Append S(reverse of C) to C C = C + "&" + S; int n = C.length(); int []longestPalindrome = new int[n]; longestPalindrome[0] = 0; int len = 0; int i = 1; // Use KMP algorithm while (i < n) { if (C.charAt(i) == C.charAt(len)) { len++; longestPalindrome[i] = len; i++; } else { if (len != 0) { len = longestPalindrome[len - 1]; } else { longestPalindrome[i] = 0; i++; } } } String ans = C.substring(0, longestPalindrome[n - 1]); return ans; } // Function to return longest palindromic // String possible from the given String // after removal of any subString static String findAns(String s) { // Initialize three Strings A, B AND F String A = ""; String B = ""; String F = ""; int i = 0; int j = s.length() - 1; int len = s.length(); // Loop to find longest subStrings // from both ends which are // reverse of each other while (i < j && s.charAt(i) == s.charAt(j)) { i = i + 1; j = j - 1; } if (i > 0) { A = s.substring(0, i); B = s.substring(len - i, len); } // Proceed to third step of our approach if (len > 2 * i) { // Remove the subStrings A and B String C = s.substring(i, (s.length() - 2 * i) + i); // Find the longest palindromic // subString from beginning of C String D = findPalindrome(C); // Find the longest palindromic // subString from end of C C = reverse(C); String E = findPalindrome(C); // Store the maximum of D and E in F if (D.length() > E.length()) { F = D; } else { F = E; } } // Find the final answer String answer = A + F + B; return answer; } static String reverse(String input) { char[] a = input.toCharArray(); int l, r = a.length - 1; for (l = 0; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.valueOf(a); } // Driver Code public static void main(String[] args) { String str = "abcdefghiedcba"; System.out.print(findAns(str) +"\n"); } } // This code is contributed by PrinciRaj1992 Python3 # Python3 implementation of the # above approach # Function to find the longest # palindrome from the start of # the string using KMP match def findPalindrome(C): S = C[::-1] # Append S(reverse of C) to C C = C[:] + '&' + S n = len(C) longestPalindrome = [0 for i in range(n)] longestPalindrome[0] = 0 ll = 0 i = 1 # Use KMP algorithm while (i < n): if (C[i] == C[ll]): ll += 1 longestPalindrome[i] = ll i += 1 else: if (ll != 0): ll = longestPalindrome[ll - 1] else: longestPalindrome[i] = 0 i += 1 ans = C[0:longestPalindrome[n - 1]] return ans # Function to return longest palindromic # string possible from the given string # after removal of any substring def findAns(s): # Initialize three strings # A, B AND F A = "" B = "" F = "" i = 0 j = len(s) - 1 ll = len(s) # Loop to find longest substrings # from both ends which are # reverse of each other while (i < j and s[i] == s[j]): i = i + 1 j = j - 1 if (i > 0): A = s[0 : i] B = s[ll - i : ll] # Proceed to third step of our approach if (ll > 2 * i): # Remove the substrings A and B C = s[i : i + (len(s) - 2 * i)] # Find the longest palindromic # substring from beginning of C D = findPalindrome(C) # Find the longest palindromic # substring from end of C C = C[::-1] E = findPalindrome(C) # Store the maximum of D and E in F if (len(D) > len(E)): F = D else: F = E # Find the final answer answer = A + F + B return answer # Driver code if __name__=="__main__": str = "abcdefghiedcba" print(findAns(str)) # This code is contributed by rutvik_56 C# // C# Implementation of the // above approach using System; class GFG{ // Function to find the longest palindrome // from the start of the String using KMP match static String findPalindrome(String C) { String S = C; S = reverse(S); // Append S(reverse of C) to C C = C + "&" + S; int n = C.Length; int []longestPalindrome = new int[n]; longestPalindrome[0] = 0; int len = 0; int i = 1; // Use KMP algorithm while (i < n) { if (C[i] == C[len]) { len++; longestPalindrome[i] = len; i++; } else { if (len != 0) { len = longestPalindrome[len - 1]; } else { longestPalindrome[i] = 0; i++; } } } String ans = C.Substring(0, longestPalindrome[n - 1]); return ans; } // Function to return longest palindromic // String possible from the given String // after removal of any subString static String findAns(String s) { // Initialize three Strings A, B AND F String A = ""; String B = ""; String F = ""; int i = 0; int j = s.Length - 1; int len = s.Length; // Loop to find longest subStrings // from both ends which are // reverse of each other while (i < j && s[i] == s[j]) { i = i + 1; j = j - 1; } if (i > 0) { A = s.Substring(0, i); B = s.Substring(len - i, i); } // Proceed to third step of our approach if (len > 2 * i) { // Remove the subStrings A and B String C = s.Substring(i, (s.Length - 2 * i)); // Find the longest palindromic // subString from beginning of C String D = findPalindrome(C); // Find the longest palindromic // subString from end of C C = reverse(C); String E = findPalindrome(C); // Store the maximum of D and E in F if (D.Length > E.Length) { F = D; } else { F = E; } } // Find the readonly answer String answer = A + F + B; return answer; } static String reverse(String input) { char[] a = input.ToCharArray(); int l, r = a.Length - 1; for (l = 0; l < r; l++, r--) { char temp = a[l]; a[l] = a[r]; a[r] = temp; } return String.Join("",a); } // Driver Code public static void Main(String[] args) { String str = "abcdefghiedcba"; Console.Write(findAns(str) +"\n"); } } // This code is contributed by Rajput-Ji JavaScript <script> // JavaScript Implementation of the // above approach // Function to find the longest palindrome // from the start of the String using KMP match function findPalindrome(C) { var S = C; S = reverse(S); // Append S(reverse of C) to C C = C + "&" + S; var n = C.length; var longestPalindrome = new Array(n).fill(0); longestPalindrome[0] = 0; var len = 0; var i = 1; // Use KMP algorithm while (i < n) { if (C[i] === C[len]) { len++; longestPalindrome[i] = len; i++; } else { if (len !== 0) { len = longestPalindrome[len - 1]; } else { longestPalindrome[i] = 0; i++; } } } var ans = C.substring(0, longestPalindrome[n - 1]); return ans; } // Function to return longest palindromic // String possible from the given String // after removal of any subString function findAns(s) { // Initialize three Strings A, B AND F var A = ""; var B = ""; var F = ""; var i = 0; var j = s.length - 1; var len = s.length; // Loop to find longest subStrings // from both ends which are // reverse of each other while (i < j && s[i] === s[j]) { i = i + 1; j = j - 1; } if (i > 0) { A = s.substring(0, i); B = s.substring(len - i, len); } // Proceed to third step of our approach if (len > 2 * i) { // Remove the subStrings A and B var C = s.substring(i, i + (s.length - 2 * i)); // Find the longest palindromic // subString from beginning of C var D = findPalindrome(C); // Find the longest palindromic // subString from end of C C = reverse(C); var E = findPalindrome(C); // Store the maximum of D and E in F if (D.length > E.length) { F = D; } else { F = E; } } // Find the readonly answer var answer = A + F + B; return answer; } function reverse(input) { var a = input.split(""); var r = a.length - 1; for (var l = 0; l < r; l++, r--) { var temp = a[l]; a[l] = a[r]; a[r] = temp; } return a.join(""); } // Driver Code var str = "abcdefghiedcba"; document.write(findAns(str)); // This code is contributed by rdtank. </script> Output: abcdeiedcba Time complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Longest palindromic string possible after removal of a substring A AmanGupta65 Follow Improve Article Tags : Strings Algorithms Pattern Searching Data Structures Programming Language DSA Arrays +3 More Practice Tags : AlgorithmsArraysData StructuresPattern SearchingStrings +1 More Similar Reads Rearrange string to obtain Longest Palindromic Substring Given string str, the task is to rearrange the given string to obtain the longest palindromic substring. Examples: Input: str = âgeeksforgeeksâOutput: eegksfskgeeorExplanation: eegksfskgee is the longest palindromic substring after rearranging the string.Therefore, the required output is eegksfskgee 9 min read Print the longest palindromic prefix of a given string Given a string str, the task is to find the longest palindromic prefix of the given string. Examples: Input: str = "abaac" Output: aba Explanation: The longest prefix of the given string which is palindromic is "aba". Input: str = "abacabaxyz" Output: abacaba Explanation: The prefixes of the given s 12 min read Longest palindromic string possible by concatenating strings from a given array Given an array of strings S[] consisting of N distinct strings of length M. The task is to generate the longest possible palindromic string by concatenating some strings from the given array. Examples: Input: N = 4, M = 3, S[] = {"omg", "bbb", "ffd", "gmo"}Output: omgbbbgmoExplanation: Strings "omg" 8 min read Largest palindromic string possible from given strings by rearranging the characters Given two strings S and P, the task is to find the largest palindrome possible string by choosing characters from the given strings S and P after rearranging the characters. Note: If many answers are possible, then find the lexicographically smallest T with maximum length. Examples: Input: S = "abad 13 min read Length of longest palindromic sub-string : Recursion Given a string S, the task is to find the length of the longest sub-string which is a palindromeExamples: Input: S = "aaaabbaa" Output: 6 Explanation: Sub-string "aabbaa" is the longest palindromic sub-string.Input: S = "banana" Output: 5 Explanation: Sub-string "anana" is the longest palindromic su 5 min read Longest Substring of 1's after removing one character Given a binary string S of length N, the task is to find the longest substring consisting of '1's only present in the string after deleting a character from the string. Examples: Input: S = "1101"Output: 3Explanation: Removing S[0], S modifies to "101". Longest possible substring of '1's is 1.Removi 12 min read Longest Palindromic Substring using hashing in O(nlogn) Given a string S, The task is to find the longest substring which is a palindrome using hashing in O(N log N) time. Input: S: âforgeeksskeegforâ, Output: âgeeksskeegâ Input: S: âGeeksâ, Output: âeeâ Hashing to Solve the Problem:The hashing approach to solving the longest palindromic substring proble 11 min read Longest Palindromic Substring using Dynamic Programming Given a string s, the task is to find the longest substring which is a palindrome. If there are multiple answers, then return the first occurrence of the longest palindromic substring from left to right.Examples:Input: s = "aaaabbaa"Output: "aabbaa"Explanation: The longest palindromic substring is " 7 min read Return a Palindromic String after removing minimum length Prefix from given String Given a string B, the task is to find the minimum length prefix of the string which, when removed and added to the end of the string, will make the string a palindrome. Return the palindromic string. If no such string exists, we need to determine that. Examples: Input: "aabb"Output: "abba"Explanatio 5 min read Minimum length of substring whose rotation generates a palindromic substring Given a string str, the task is to find the minimum length of substring required to rotate that generates a palindromic substring from the given string. Examples: Input: str = "abcbd" Output: 0 Explanation: No palindromic substring can be generated. There is no repeated character in the string. Inpu 7 min read Like