当前位置:网站首页>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];
}
边栏推荐
- Here comes the nearest chance to Ali
- Several commands not commonly used in MySQL
- 从零开发 stylelint规则(插件)
- Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
- Projet Web de déploiement du serveur Cloud
- SHELL
- requests. The difference between session () sending requests and using requests to send requests directly
- Dao -- a beautiful new world?
- How to use unmarshaljson
- Attempt to redefine 'timeout' at line 2 solution
猜你喜欢

云服务器部署 Web 项目

Gestion des utilisateurs de la base de données MySQL

MySQL 索引

Mysql database user management

Codeforces C. Andrew and Stones

Golang之手写web框架
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor
![[GPU] basic operation of GPU (I)](/img/ce/0ca8c63525038fea64c40aabd17fc6.jpg)
[GPU] basic operation of GPU (I)

MySQL advanced SQL statement

UE4_ Editor UMG close window cannot destroy UMG immediately
随机推荐
MySQL存储系统
Configure the user to log in to the device through telnet -- AAA local authentication
Do you know how to show the health code in only 2 steps
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime
网络基础知识
JS prototype chain object function relationship
About modifying dual system default startup item settings
[deep learning] data segmentation
Network basics
观察者模式、状态模式在实际工作中的使用
What indicators should safety service engineers pay attention to in emergency response?
VIM view file code
Common NPM install errors
Installation and initialization of MariaDB database
1380. lucky numbers in matrices
Use of tornado template
Leetcode search insert location
Swoole process model diagram
Sword finger offer 29 Print matrix clockwise
Common mistakes daily practice 01