Minimum insertions to form shortest palindrome
Given a string S, determine the least number of characters that should be added on to the left side of S so that the complete string becomes a palindrome.
Examples:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: S = "LOL" Output: 0 LOL is already a palindrome Input: S = "JAVA" Output: 3 We need to add 3 characters to form AVAJAVA.
The idea is to find the longest palindromic prefix of given string. The count of characters after the prefix is our answer. The longest palindromic prefix can be found by looping from last char to first char. For example, in “JAVA”, the longest palindromic prefix is “J”, so we need to add remaining 3 at the beginning characters to form palindrome.
C++
// C++ program to find minimum number of insertions// on left side to form a palindrome.#include <bits/stdc++.h>using namespace std;// Returns true if a string str[st..end] is palindromebool isPalin(char str[], int st, int end){ while (st < end) { if (str[st] != str[end]) return false; st++; end--; } return true;}// Returns count of insertions on left side to make// str[] a palindromeint findMinInsert(char str[], int n){ // Find the largest prefix of given string // that is palindrome. for (int i=n-1; i>=0; i--) { // Characters after the palindromic prefix // must be added at the beginning also to make // the complete string palindrome if (isPalin(str, 0, i)) return (n-i-1); }}// Driver programint main(){ char Input[] = "JAVA"; printf("%d", findMinInsert(Input, strlen(Input))); return 0;} |
Java
// Java program to find minimum number// of insertions on left side to form// a palindrome.import java.util.*; class GFG{ // Returns true if a string// str[st..end] is palindromestatic boolean isPalin(char []str, int st, int end){ while (st < end) { if (str[st] != str[end]) return false; st++; end--; } return true;} // Returns count of insertions on// left side to make str[] a palindromestatic int findMinInsert(char []str, int n){ // Find the largest prefix of given // string that is palindrome. for(int i = n - 1; i >= 0; i--) { // Characters after the palindromic // prefix must be added at the // beginning also to make the // complete string palindrome if (isPalin(str, 0, i)) return (n - i - 1); } return 0;} // Driver Codepublic static void main(String []args){ char []Input = "JAVA".toCharArray(); System.out.println(findMinInsert(Input, Input.length));}} // This code is contributed by pratham76 |
Python3
# Python3 program to find# minimum number of insertions# on left side to form a palindrome.# Returns true if a string# str[st..end] is palindromedef isPalin(str, st, end): while (st < end): if (str[st] != str[end]): return False st += 1 end--1 return True# Returns count of insertions# on left side to make# str[] a palindromedef findMinInsert(str, n): # Find the largest # prefix of given string # that is palindrome. for i in range(n-1 ,-1, -1): # Characters after the # palindromic prefix must # be added at the beginning # also to make the complete # string palindrome if (isPalin(str, 0, i)): return (n - i - 1)# Driver CodeInput = "JAVA"print(findMinInsert(Input, len(Input)))# This code is contributed# by Smitha |
C#
// C# program to find minimum number// of insertions on left side to form// a palindrome.using System;using System.Text;class GFG{ // Returns true if a string// str[st..end] is palindromestatic bool isPalin(char []str, int st, int end){ while (st < end) { if (str[st] != str[end]) return false; st++; end--; } return true;} // Returns count of insertions on// left side to make str[] a palindromestatic int findMinInsert(char []str, int n){ // Find the largest prefix of given string // that is palindrome. for(int i = n - 1; i >= 0; i--) { // Characters after the palindromic // prefix must be added at the // beginning also to make the // complete string palindrome if (isPalin(str, 0, i)) return (n - i - 1); } return 0;} // Driver Codepublic static void Main(string []args){ char []Input = "JAVA".ToCharArray(); Console.Write(findMinInsert(Input, Input.Length));}}// This code is contributed by rutvik_56 |
Javascript
<script>// javascript program to find minimum number of insertions// on left side to form a palindrome.// Returns true if a string str[st..end] is palindromefunction isPalin(str,st,end){ while (st < end) { if (str[st] != str[end]) return false; st++; end--; } return true;}// Returns count of insertions on left side to make// str[] a palindromefunction findMinInsert(str,n){ // Find the largest prefix of given string // that is palindrome. for (let i = n - 1; i >= 0; i--) { // Characters after the palindromic prefix // must be added at the beginning also to make // the complete string palindrome if (isPalin(str, 0, i)) return (n - i - 1); }} let Input = "JAVA"; document.write(findMinInsert(Input,Input.length)); // This code is contributed by vaibhavrabadiya07. </script> |
Output:
3
Time Complexity: O(n2)
Thanks to Utkarsh Trivedi for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


