LeetCode //C - 72. Edit Distance

72. Edit Distance

Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.

You have the following three operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character
     
Example 1:

Input: word1 = “horse”, word2 = “ros”
Output: 3
Explanation:
horse -> rorse (replace ‘h’ with ‘r’)
rorse -> rose (remove ‘r’)
rose -> ros (remove ‘e’)

Example 2:

Input: word1 = “intention”, word2 = “execution”
Output: 5
Explanation:
intention -> inention (remove ‘t’)
inention -> enention (replace ‘i’ with ‘e’)
enention -> exention (replace ‘n’ with ‘x’)
exention -> exection (replace ‘n’ with ‘c’)
exection -> execution (insert ‘u’)

Constraints:
  • 0 <= word1.length, word2.length <= 500
  • word1 and word2 consist of lowercase English letters.

From: LeetCode
Link: 72. Edit Distance


Solution:

Ideas:
  • Initialize base cases of empty strings
  • Use dynamic programming to fill table
  • Minimum operations at any point is 1 (for replace) + minimum of insert, delete or replace from previous state
Code:
int min(int a, int b){
   
    return a < b ? a : b;
}

int minDistance(char* word1, char* word2) {
   
    int m = strlen(word1);
    int n = strlen(word2);
    
    int dp[m+1][n+1];
    
    for(int i = 0; i <= m; i++) {
   
        dp[i][0] = i;
    }
    
    for(int j = 0; j <= n; j++) {
   
        dp[0][j] = j;
    }
    
    for(int i = 1; i <= m; i++) {
   
        for(int j = 1; j <= n; j++) {
   
            if(word1[i-1] == word2[j-1]) {
   
                dp[i][j] = dp[i-1][j-1]; 
            } else {
   
                dp[i][j] = 1 + min(dp[i][j-1], min(dp[i-1][j], dp[i-1][j-1]));
            }
        }
    }
    
    return dp[m][n]; 
}

相关推荐

  1. 面试经典150题(72-77)

    2023-12-06 03:28:05       55 阅读
  2. 商城数据库88章表72~75

    2023-12-06 03:28:05       35 阅读
  3. 【嵌入式开发】72

    2023-12-06 03:28:05       42 阅读
  4. day<span style='color:red;'>72</span>Html

    day72Html

    2023-12-06 03:28:05      50 阅读
  5. leetcode 72.编辑距离

    2023-12-06 03:28:05       47 阅读
  6. LeetCode //C - 72. Edit Distance

    2023-12-06 03:28:05       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 03:28:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 03:28:05       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 03:28:05       87 阅读
  4. Python语言-面向对象

    2023-12-06 03:28:05       96 阅读

热门阅读

  1. 鼠标移入移出事件

    2023-12-06 03:28:05       58 阅读
  2. Gson与FastJson详解

    2023-12-06 03:28:05       54 阅读
  3. qt 5.15.2 网络文件下载功能

    2023-12-06 03:28:05       58 阅读
  4. 卷积神经网络训练情感分析

    2023-12-06 03:28:05       58 阅读
  5. 11-鸿蒙4.0学习之页面之间的参数传递

    2023-12-06 03:28:05       49 阅读
  6. RabbitMQ之ttl(过期消息)解读

    2023-12-06 03:28:05       58 阅读
  7. es常用查询编辑

    2023-12-06 03:28:05       49 阅读
  8. React-hook-form-mui (二):表单数据处理

    2023-12-06 03:28:05       52 阅读
  9. 零基础学Python的第六天||字符串(3)

    2023-12-06 03:28:05       62 阅读