Open In App

Edit Distance in C

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Edit Distance problem is a classic dynamic programming problem that involves transforming one string into another with the minimum number of operations. In this article, we will learn how to solve this problem in C language for given two strings str1 of length M and str2 of length N, and the following operations:

  • INSERT: Insert any character.
  • REMOVE: Remove a character.
  • REPLACE: Replace a character.

Our goal is to find the minimum number of edits required to convert str1 into str2.

For Example:

Input:
str1 = "GEEXSFRGEEKKS"
str2 = "GEEKSFORGEEKS"

Output:
3
Edit-Distance-Illustration-(1)-copy
Edit Distance

To convert str1 into str2, the minimum operations required are:

  • Replace 'X' with 'K'.
  • Insert 'O' between 'F' and 'R'.
  • Remove the second last character 'K'.

Method 1: Using Recursion

The recursive approach involves comparing characters from the end of both strings and performing operations accordingly. If characters match, move to the next pair. If they don't, consider all three operations (insert, remove, replace) and choose the minimum.

Approach

  • Base Condition: If either string is empty, return the length of the other string (insertion or deletion).
  • Recursive Steps:
    • If the last characters match:
      • Recursively call EditDistance(M-1, N-1).
    • If the last characters don't match, consider three operations:
      • Insert str1[N-1] at the end of str2: EditDistance(M, N-1).
      • Replace str2[M-1] with str1[N-1]: EditDistance(M-1, N-1).
      • Remove str2[M-1]: EditDistance(M-1, N).

Below is the recursive tree

Recursion-Tree-for-Edit-Distance-(1)

C Program to Solve Edit Distance Using Recursion

The below program demonstrates how we can solve edit distance problem using recursive approach in C.

C
// C Program to Solve Edit Distance Using Recursion

#include <stdio.h>
#include <string.h>

// Function to return the minimum of three integers
int min(int a, int b, int c)
{
    return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
}

// Recursive function to find the edit distance between two
// strings
int editDistanceRecursive(char* X, char* Y, int m, int n)
{
    // If first string is empty, only option is to insert
    // all characters of second string
    if (m == 0)
        return n;

    // If second string is empty, only option is to remove
    // all characters of first string
    if (n == 0)
        return m;

    // If last characters are same, ignore last characters
    // and recur for remaining strings
    if (X[m - 1] == Y[n - 1])
        return editDistanceRecursive(X, Y, m - 1, n - 1);

    // If last characters are different, consider all three
    // operations on last character of first string,
    // recursively compute minimum cost for all three
    // operations and take minimum of three values.
    return 1
           + min(editDistanceRecursive(X, Y, m,
                                       n - 1), // Insert
                 editDistanceRecursive(X, Y, m - 1,
                                       n), // Remove
                 editDistanceRecursive(X, Y, m - 1,
                                       n - 1) // Replace
           );
}

int main()
{
    // Define the two strings to compare
    char X[] = "GEEXSFRGEEKKS";
    char Y[] = "GEEKSFORGEEKS";

    // Calculate the lengths of the strings
    int m = strlen(X);
    int n = strlen(Y);

    // Print the minimum edit distance between the two
    // strings
    printf("Minimum edit distance is %d\n",
           editDistanceRecursive(X, Y, m, n));

    return 0;
}

Output
Minimum edit distance is 3

Time Complexity: O(3^m), when length of “str1” >= length of “str2” and O(3^n), when length of “str2” > length of “str1”. Here m=length of “str1 and n=length of “str2”
Auxiliary Space: O(m), when length of “str1” >= length of “str2” and O(n), when length of “str2” > length of “str1”. Here m=length of “str1 and n=length of “str2”

Method 2: Using Dynamic Programming (Memoization)

To optimize the recursive approach, we can store the results of subproblems to avoid redundant calculations, for this we will use a 2D array dp where dp[i][j] represents the minimum edit distance between str1[0..i-1] and str2[0..j-1].

Overlapping-subproblems-in-the-Edit-Distance

Approach

  • Initialization:
    • Create a 2D array dp of size (M+1) x (N+1) to store the results of subproblems.
    • Initialize dp[i][0] = i for all i from 0 to M.
    • Initialize dp[0][j] = j for all j from 0 to N.
  • Filling the DP Table:
    • For each pair (i, j):
      • If the characters str1[i-1] and str2[j-1] are the same, then dp[i][j] = dp[i-1][j-1].
      • If the characters are different, consider all three operations and take the minimum:
        • Insert: dp[i][j] = dp[i][j-1] + 1
        • Remove: dp[i][j] = dp[i-1][j] + 1
        • Replace: dp[i][j] = dp[i-1][j-1] + 1

C Program to Solve Edit Distance Using Dynamic Programming (Memoization)

The below program demonstrates how we can solve edit distance problem using Dynamic Programming (Memoization) in C.

C
// C Program to Solve Edit Distance Using Dynamic
// Programming (Memoization)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to find the minimum of three integers
int min(int x, int y, int z)
{
    if (x < y && x < z)
        return x;
    else if (y < x && y < z)
        return y;
    else
        return z;
}

// Function to calculate the minimum edit distance using
// dynamic programming
int minDis(char* s1, char* s2, int n, int m)
{
    // Declare a 2D array to store the dynamic programming
    // table
    int dp[n + 1][m + 1];

    // Fill the dynamic programming table
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            // If the first string is empty, the only option
            // is to insert all characters of the second
            // string
            if (i == 0)
                dp[i][j] = j;
            // If the second string is empty, the only
            // option is to remove all characters of the
            // first string
            else if (j == 0)
                dp[i][j] = i;
            // If the last characters are the same, ignore
            // the last characters and recur for the
            // remaining strings
            else if (s1[i - 1] == s2[j - 1])
                dp[i][j] = dp[i - 1][j - 1];
            // If the last characters are different,
            // consider all three operations and find the
            // minimum
            else
                dp[i][j] = 1
                           + min(dp[i][j - 1], dp[i - 1][j],
                                 dp[i - 1][j - 1]);
        }
    }
    // Return the result from the dynamic programming table
    return dp[n][m];
}

int main()
{
    // Define the two strings to compare
    char* str1 = "GEEXSFRGEEKKS";
    char* str2 = "GEEKSFORGEEKS";

    // Calculate the lengths of the strings
    int n = strlen(str1), m = strlen(str2);

    // Print the minimum edit distance between the two
    // strings
    printf("%d\n", minDis(str1, str2, n, m));

    return 0;
}

Output
3

Time Complexity: O(m x n) 
Auxiliary Space: O( m *n)+O(m+n) , (m*n) extra array space and (m+n) recursive stack space.

Method 3: Using Dynamic Programming (Bottom-Up)

The bottom-up approach is similar to memoization but fills the DP table iteratively.

Approach

  • Initialization:
    • Create a 2D array dp of size (M+1) x (N+1) to store the results of subproblems.
    • Initialize dp[i][0] = i for all i from 0 to M.
    • Initialize dp[0][j] = j for all j from 0 to N.
  • Filling the DP Table:
    • For each pair (i, j):
      • If the characters str1[i-1] and str2[j-1] are the same, then dp[i][j] = dp[i-1][j-1].
      • If the characters are different, consider all three operations and take the minimum:
        • Insert: dp[i][j] = dp[i][j-1] + 1
        • Remove: dp[i][j] = dp[i-1][j] + 1
        • Replace: dp[i][j] = dp[i-1][j-1] + 1

C Program to Solve Edit Distance Using Dynamic Programming (Bottom-Up)

The below program demonstrates how we can solve edit distance problem using Dynamic Programming (Bottom-Up) in C.

C
// C Program to Solve Edit Distance Using Dynamic
// Programming (Bottom-Up)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Utility function to find the minimum of three numbers
int min(int x, int y, int z)
{
    if (x < y && x < z)
        return x;
    else if (y < x && y < z)
        return y;
    else
        return z;
}

// Function to calculate the minimum edit distance using
// bottom-up dynamic programming
int minDis(char* s1, char* s2, int n, int m)
{
    // Declare a 2D array to store the dynamic programming
    // table
    int dp[n + 1][m + 1];

    // Initialize the dp table
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            // If the first string is empty, the only option
            // is to insert all characters of the second
            // string
            if (i == 0) {
                dp[i][j] = j;
            }
            // If the second string is empty, the only
            // option is to remove all characters of the
            // first string
            else if (j == 0) {
                dp[i][j] = i;
            }
            // If the last characters are the same, ignore
            // the last characters and recur for the
            // remaining strings
            else if (s1[i - 1] == s2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1];
            }
            // If the last characters are different,
            // consider all three operations and find the
            // minimum
            else {
                dp[i][j] = 1
                           + min(dp[i - 1][j], dp[i][j - 1],
                                 dp[i - 1][j - 1]);
            }
        }
    }

    // Return the result from the dynamic programming table
    return dp[n][m];
}

int main()
{
    // Define the two strings to compare
    char s1[] = "GEEXSFRGEEKKS";
    char s2[] = "GEEKSFORGEEKS";

    // Calculate the lengths of the strings
    int n = strlen(s1);
    int m = strlen(s2);

    // Print the minimum edit distance between the two
    // strings
    printf("%d\n", minDis(s1, s2, n, m));

    return 0;
}

Output
3

Time Complexity: O(m x n) 
Auxiliary Space: O(m x n)

Method 4: Using Dynamic Programming (Space-Optimized)

The space-optimized approach reduces the space complexity by using only two rows of the DP table instead of the entire table.

Approach

  • Initialization:
    • Create two arrays prev and curr of size (N+1) to store the results of the current and previous rows.
    • Initialize prev[j] = j for all j from 0 to N.
  • Filling the DP Table:
    • For each pair (i, j):
      • If the characters str1[i-1] and str2[j-1] are the same, then curr[j] = prev[j-1].
      • If the characters are different, consider all three operations and take the minimum:
        • Insert: curr[j] = curr[j-1] + 1
        • Remove: curr[j] = prev[j] + 1
        • Replace: curr[j] = prev[j-1] + 1
    • Swap prev and curr after processing each row.

C Program to Solve Edit Distance Using Dynamic Programming (Space-Optimized)

The below program demonstrates how we can solve edit distance problem using Dynamic Programming (Space-Optimized) in C.

C
// C Program to Solve Edit Distance Using Dynamic
// Programming (Space-Optimized)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Utility function to find the minimum of three numbers
int min(int x, int y, int z)
{
    if (x < y && x < z)
        return x;
    else if (y < x && y < z)
        return y;
    else
        return z;
}

// Function to calculate the minimum edit distance using
// space-optimized dynamic programming
int minDis(char* s1, char* s2, int n, int m)
{
    // Declare two arrays to store the current and previous
    // rows of the dp table
    int prev[m + 1], curr[m + 1];

    // Initialize the prev array
    for (int j = 0; j <= m; j++) {
        prev[j] = j;
    }

    // Iterate over each character of the first string
    for (int i = 1; i <= n; i++) {
        // Initialize the first element of the current row
        curr[0] = i;
        // Iterate over each character of the second string
        for (int j = 1; j <= m; j++) {
            // If the characters are the same, take the
            // diagonal value
            if (s1[i - 1] == s2[j - 1]) {
                curr[j] = prev[j - 1];
            }
            // If the characters are different, take the
            // minimum of the three operations
            else {
                curr[j] = 1
                          + min(prev[j], curr[j - 1],
                                prev[j - 1]);
            }
        }
        // Copy the current row to the previous row for the
        // next iteration
        memcpy(prev, curr, (m + 1) * sizeof(int));
    }

    // Return the result from the previous row
    return prev[m];
}

int main()
{
    // Define the two strings to compare
    char s1[] = "GEEXSFRGEEKKS";
    char s2[] = "GEEKSFORGEEKS";

    // Calculate the lengths of the strings
    int n = strlen(s1);
    int m = strlen(s2);

    // Print the minimum edit distance between the two
    // strings
    printf("%d\n", minDis(s1, s2, n, m));

    return 0;
}

Output
3

Time Complexity: O(M x N) where M and N are lengths of the string 
Auxiliary Space: O( N ), Length of the str2


Article Tags :

Similar Reads