
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find the minimum edit distance between two strings in C++
Suppose we have two words S and T, we have to find the minimum number of operations needed to convert from S to T. The operations can be of three types, these are
- insert a character,
- delete a character
- replace a character.
So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.
To solve this, we will follow these steps −
n := size of s, m := size of t,
create an array dp of size n + 1
for i in range 0 to n
dp[i] := new array of size m + 1
for j in range 0 to m:
dp[i, j] := 0
if i = 0, then dp[i,j] = j
otherwise when j = 0, then dp[i, j] := i
s := blank space and concatenate s, t := blank space and concatenate t
for i in range 1 to n
for j in range 1 to m
if s[i] is not t[j], then dp[i, j] := 1 + min of dp[i – 1, j], dp[i, j - 1], dp[i – 1, j – 1]
otherwise dp[i, j] := dp[i – 1, j – 1]
return dp[n, m]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int minDistance(string s, string t) { int n = s.size(); int m =t.size(); int** dp = new int*[n+1]; for(int i =0;i<=n;i++){ dp[i] = new int[m+1]; for(int j=0;j<=m;j++){ dp[i][j]=0; if(i==0)dp[i][j]=j; else if(j==0)dp[i][j] = i; } } s = " " + s; t = " " + t; for(int i =1;i<=n;i++){ for(int j = 1;j<=m;j++){ if(s[i] !=t[j]){ dp[i][j] = 1+min({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]}); }else{ dp[i][j] = dp[i-1][j-1]; } } } return dp[n][m]; } }; main(){ Solution ob; cout << (ob.minDistance("fluctuate", "evaluate")); }
Input
"fluctuate" "evaluate"
Output
5
- Related Articles
- Check if edit distance between two strings is one in Python
- Program to check two strings are 0 or 1 edit distance away or not in Python
- Find the minimum distance between two numbers in C++
- Hamming Distance between two strings in JavaScript
- Program to find minimum deletions to make strings strings in Python
- Program to Find the Shortest Distance Between Two Points in C++
- Program to find minimum total distance between house and nearest mailbox in Python
- Program to find minimum distance of two given words in a text in Python
- C++ program to find the shortest distance between two nodes in BST
- Program to find minimum difference between two elements from two lists in Python
- Program to find minimum distance to the target element using Python
- Edit Distance in C++
- Edit Distance\n
- Program to find out distance between two nodes in a binary tree in Python
- C program to calculate distance between two points
