How to solve Edit Distance Problem?

A Quick Guide

The edit distance problem is the minimum number of insertions, deletions, or replacements required to convert one string to another.

Get started now

Edit Distance Problem

Given two strings A & B, find minimum no. of steps required to convert A to B. You have following 3 operations permitted on a word:    - Insert a character   - Delete a character   - Replace a character

Ready to explore different approaches?

Problem Statement

Example- Input: A = “abad”, B = “abac”  Output: 1  Explanation: Operation 1: Replace d with c.

Check out more examples

To solve the problem, a recursive approach is used. All the characters in both strings are traversed one by one, either from left or right end, & the given operations are applied.

Check out the algorithm

Approach 1: Recursion

Time Complexity: O(3^(N * M)), where N and M are the lengths of the 1st and 2nd string.  Space Complexity: O(N + M).

Want to see code implementation?

The idea is to use tabulation method to implement dynamic programming, which is the most efficient way to solve this particular problem.

Approach 2: Dynamic Programming

Check out the algorithm

Time Complexity: O(N * M), where N and M are the lengths of the 1st and 2nd string.  Space Complexity: O(N * M).

Want to see code implementation?

Start your journey now and learn how to successfully solve edit distance problem.

Ready to level up your coding skills?

Step Up Your Game with InterviewBit Web Stories

Don't miss out on the chance to upskill yourself with IntervewBit's engaging web stories.