JavaScript Program to Find Minimum Insertions to Form a Palindrome


We are given a string and we have to find the minimum number of different character that we need to insert in the given string at any place so that the final string will be palindrome. A palindrome is a string that is just equal to the reverse of it. This problem is of dynamic programming, so we will first go for the recursive approach, then we will memorize it, and at the end we will see the tabulation of the memorization approach.

Recursive Approach

Example

const max = 1e5; // defining the upper limit 
// function to find the minimum of two number as it is not present in the c language 
function findMin(a, b){ 
   if(a < b){
      return a;
   } else{
       return b;
   }
}
// creating the function for finding the required answer we will make recursive calls to it 
function findAns(str,start,end){
   // base condition
   if (start > end){
      return max;
   }
   else if(start == end){
      return 0;
   }
   else if (start == end - 1){
      if(str[start] == str[end]){
         return 0;
      }
      else return 1;
   }	
   // check if both start and end characters are the same make calls on the basis of that 
   if(str[start] == str[end]){
      return findAns(str,start+1, end-1);
   } else{
       return 1+ findMin(findAns(str,start,end-1), findAns(str,start+1,end));
   }
}
// given inputs
var str = "thisisthestring"; // given string
console.log("The minimum number of insertions required to form the palindrome is: " + findAns(str,0,str.length-1));

Output

The minimum number of insertions required to form the palindrome is: 8

Time and Space Complexity

The time complexity of the above code is O(2^N), as we are making choice for each insertion, where N is the size of the given string.

The space complexity of the above code is O(N), that is used in the recursive calls.

Memoization Approach

Example

const max = 1e5; // defining the upper limit 
var memo = new Array(1005); // array to store the recursion results
// function to find the minimum of two number as it is not present in the c language 
function findMin(a, b){ 
   if(a < b){
      return a;
   } else{
      return b;
   }
}  
// creating function for finding the required answer we will make recursive calls to it 
function findAns(str,start,end){
   // base condition
   if (start > end){
      return max;
   }
   else if(start == end){
       return 0;
   }
   else if (start == end - 1){
      if(str[start] == str[end]){
         return 0;
      }
      else return 1;
   }
        
   if(memo[start][end] != -1){
      return memo[start][end];
   }
        
   // check if both start and end characters are the same make calls on the basis of that 
    if(str[start] == str[end]){
       memo[start][end] =  findAns(str,start+1, end-1);
   } else{
      memo[start][end] = 1+ findMin(findAns(str,start,end-1), findAns(str,start+1,end));
   }    
   return memo[start][end];
}
// given inputs
var str = "thisisthestring"; // given string
// initialzie the memo array 
for(var i=0; i< 1005; i++){
   memo[i] = new Array(1005);
   for(var j = 0; j<1005; j++){
      memo[i][j] = -1;
   }
}
console.log("The minimum number of insertions required to form the palindrome is: " + findAns(str,0,str.length-1));

Output

The minimum number of insertions required to form the palindrome is: 8

Time and Space Complexity

The time complexity of the above code is O(N^2), as we are storing the results that are already calculated.

The space complexity of the above code is O(N^2), because we have used the extra space here.

Dynamic Programming Approach

Example

const max = 1e5; // defining the upper limit 
var memo = new Array(1005); // array to store the recursion results
// function to find the minimum of two number as it is not present in the c language 
function findMin(a, b){ 
   if(a < b){
      return a;
   } else{
      return b;
   }
}
// creating a function for finding the required answer we will make recursive calls to it 
function findAns(str, len){
        
   // filling the table by traversing over the string 
   for (var i = 1; i < len; i++){
      for (var start= 0, end = i; end < len; start++, end++){
         if(str[start] == str[end]){
            memo[start][end] = memo[start+1][end-1];
         } else{
             memo[start][end] = 1 + findMin(memo[start][end-1], memo[start+1][end]);
             }
          }
       }
       // return the minimum numbers of interstion required for the complete string 
   return memo[0][len-1];
}
// given inputs
var str = "thisisthestring"; // given string
// initialzie the memo array 
for(var i=0; i< 1005; i++){
   memo[i] = new Array(1005);
   for(var j = 0; j<1005; j++){
      memo[i][j] = 0;
   }
}
console.log("The minimum number of insertions required to form the palindrome is: " + findAns(str,str.length));

Output

The minimum number of insertions required to form the palindrome is: 8

Time and Space Complexity

The time complexity of the above code is O(N^2), as we are using the nested for loops here.

The space complexity of the above code is O(N^2), because we have used the extra space here.

Conclusion

In this tutorial, we have implemented three approaches from recursion to memorization and then tabulation in the JavaScript programming language to find the number of minimum insertions required to make the given string a palindrome. A palindrome is a string that is just equal to the reverse of it or we can read the character from the front or behind will be same.

Updated on: 12-Jul-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements