当前位置:网站首页>583. deleting two strings - Dynamic Planning
583. deleting two strings - Dynamic Planning
2022-06-30 06:04:00 【Mr Gao】
583. Deletion of two strings
Give two words word1 and word2 , Return makes word1 and word2 The same minimum number of steps required .
Every step You can delete a character from any string .
Example 1:
Input : word1 = “sea”, word2 = “eat”
Output : 2
explain : The first step will be “sea” Turn into “ea” , The second step will be "eat " Turn into “ea”
Example 2:
Input :word1 = “leetcode”, word2 = “etco”
Output :4
This problem can be transformed into solving the longest common subsequence , Using dynamic programming algorithms , More appropriate :
int minDistance(char * word1, char * word2){
int n=strlen(word1),m=strlen(word2);
int dp[n+1][m+1];
int i,j;
for(i=0;i<=m;i++){
dp[0][i]=0;
}
for(i=0;i<=n;i++){
dp[i][0]=0;
}
for(i=1;i<=n;i++){
char ch1=word1[i-1];
for(j=1;j<=m;j++){
char ch2=word2[j-1];
if(ch2==ch1){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
if(dp[i-1][j]>dp[i][j-1]){
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=dp[i][j-1];
}
}
// printf("%d index %d %d ",dp[i][j],i,j);
}
}
return n-dp[n][m]+m-dp[n][m];
}
边栏推荐
- Résoudre le problème de décompiler la compilation normale
- STM32F103 series controlled OLED IIC 4-pin
- ES6箭头函数
- Acwing winter vacation daily question 2022 punch in day 11
- MySQL transaction
- [road of system analyst] collection of wrong topics in Project Management Chapter
- MySQL高级SQL语句
- Do you know how to show the health code in only 2 steps
- How to use unmarshaljson
- [GPU] basic operation
猜你喜欢

Here comes the nearest chance to Ali

Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程

The average salary of software testing in 2022 has been released. Have you been averaged?

Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion

PC viewing WiFi password

Intelligent deodorizer embedded development

接口中方法详解

MySQL高级SQL语句

Tornado frame foundation

Installation and initialization of MariaDB database
随机推荐
09- [istio] istio service entry
Several commands not commonly used in MySQL
动态规划--怪盗基德的滑翔翼
1380. lucky numbers in matrices
The average salary of software testing in 2022 has been released. Have you been averaged?
Golang之手写web框架
【数据库】事务
OSPF - authentication and load balancing summary (including configuration commands)
Record a problem tracking of excessive load
MySQL日志管理、数据备份、恢复
UE4_ Editor UMG close window cannot destroy UMG immediately
Related applications of priority queue
Attempt to redefine 'timeout' at line 2 solution
Cisco VXLAN配置
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
Decompilation normal decompilation problems. Solve them yourself
反編譯正常回編譯出現問題自己解决辦法
[GPU] basic operation of GPU (I)
Sword finger offer 29 Print matrix clockwise
[database] transaction