当前位置:网站首页>1312. Minimum number of inserts to make a string a palindrome string
1312. Minimum number of inserts to make a string a palindrome string
2022-07-22 17:27:00 【Xiao Lu wants to brush the questions】
List of articles
1312. The minimum number of inserts to make a string a palindrome string
Give you a string s , You can insert any character in any position of the string every time .
Please return to let s To become a palindrome Minimum number of operations .
「 Palindrome string 」 It's the same string for both forward and reverse reading .
Example 1:
Input :s = “zzazz”
Output :0
explain : character string “zzazz” It's already a palindrome , So you don't need to do any insertion .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-insertion-steps-to-make-a-string-palindrome
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
Dynamic programming , Range try model
dp[i][j]:str from i To j How many characters need to be inserted to form a palindrome string The lower left half is useless , Fill in the diagonal 0
Penultimate diagonal L==R-1
Equal is equal 0 individual , Don't wait 1 individual
General position :
dp[i][j] Yes 3 In this case
1) hypothesis i To j-1 Are already palindromes , We just need to i-1 Position plus j The characters of
dp[i][j-1]+1
2) hypothesis i+1 To j Are already palindromes , We just need to i Position plus j The characters of
namely dp[i+1][j]+1
3) hypothesis i+1 To j-1 Are already palindromes , also i And j equal
dp[i][j]
Code
class Solution {
public int minInsertions(String s) {
char[] str=s.toCharArray();
int n=str.length;
if(n==0||n==1){
return 0;
}
int[][] dp=new int[n][n];
for(int i=0;i<n-1;i++){
dp[i][i+1]=str[i]==str[i+1]?0:1;
}
for(int i=n-3;i>=0;i--){
for(int j=i+2;j<n;j++){
dp[i][j]=Math.min(dp[i+1][j],dp[i][j-1])+1;
if(str[i]==str[j]){
dp[i][j]=Math.min(dp[i][j],dp[i+1][j-1]);
}
}
}
return dp[0][n-1];
}
}
边栏推荐
猜你喜欢
生产线平衡优化毕业论文【flexsim仿真】
Server network performance tuning tool
LCD notes (1) LCD hardware operation principle of different interfaces
Recommendation of selected topics for completed topics of Electronic Information Engineering
What if win11 encounters a problem and needs to restart?
JS的DOM操作——阻止事件冒泡和阻止默认事件
PlayBook introduction
MVC模式和三层架构
牛客网刷题1【最小步数Fibonacci数列】
重装Win11系统如何在线一键进行?
随机推荐
Playbook 介绍
es6相关面试题2
Activity recommendation | Apache pulsar's exploration and practice in vivo will be broadcast soon
Is it safe to open an account with the VIP link of Galaxy Securities? What is the lowest commission?
mysql约束之_非空约束
Share our first otherside trip
分支语句和循环语句
mysql约束之_自增长约束_auto_increment
matlab混频器的实现
【图文并茂】在线一键重装win7系统详细教程
STL map
NFS network file system
抖音怎么注销账号?操作流程步骤有哪些?
Cans (kuangbin topic)
【转载】UE4 面试基础知识(二)
对称式加密与非对称式加密的对比
服务器buffer/cache 的产生原因和释放buffer/cache
MVC模式和三层架构
[pictures and texts] detailed tutorial of online one click reinstallation of win7 system
Pytorch deep learning practice-b station Liu erden-day1