查找构成回文串所需最小插入次数的 JavaScript 程序


给定一个字符串,我们需要找到需要插入到给定字符串中的最小不同字符数,以便最终字符串成为回文串。回文串是一个与自身反转相同的字符串。这个问题属于动态规划问题,因此我们将首先采用递归方法,然后对其进行记忆化,最后查看记忆化方法的表格化。

递归方法

示例

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));

输出

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

时间和空间复杂度

上述代码的时间复杂度为 O(2^N),因为我们对每次插入都进行选择,其中 N 是给定字符串的大小。

上述代码的空间复杂度为 O(N),用于递归调用。

记忆化方法

示例

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));

输出

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

时间和空间复杂度

上述代码的时间复杂度为 O(N^2),因为我们存储了已经计算的结果。

上述代码的空间复杂度为 O(N^2),因为我们在这里使用了额外的空间。

动态规划方法

示例

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));

输出

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

时间和空间复杂度

上述代码的时间复杂度为 O(N^2),因为我们在这里使用了嵌套 for 循环。

上述代码的空间复杂度为 O(N^2),因为我们在这里使用了额外的空间。

结论

在本教程中,我们使用 JavaScript 编程语言实现了从递归到记忆化再到表格化的三种方法,以查找使给定字符串成为回文串所需的最小插入次数。回文串是一个与自身反转相同的字符串,或者我们可以从前面或后面读取字符,结果都相同。

更新于:2023年7月12日

浏览量:136

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.