
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Edit Distance
There are two strings given. The first string is the source string and the second string is the target string. In this program, we have to find how many possible edits are needed to convert first string to the second string.
The edit of strings can be either Insert some elements, delete something from the first string or modify something to convert into the second string.
Input and Output
Input: Two strings to compare. string 1: Programming string 2: Programs Output: Enter the initial string: Programming Enter the final string: Programs The number of changes required to convert Programming to Programs is 4
Algorithm
editCount(initStr, finalStr, initLen, finalLen)
Input − The initial and final string and their lengths.
Output − Number of edits are required to make initStr to finalStr.
Begin if initLen = 0, then return finalLen if finalLen := 0, then return initLen if initStr[initLen - 1] = finalStr[finalLen - 1], then return editCount(initStr, finalStr, initLen – 1, finalLen - 1) answer := 1 + min of (editCount(initStr, finalStr, initLen , finalLen - 1)), (editCount(initStr, finalStr, initLen – 1, finalLen ), (editCount(initStr, finalStr, initLen – 1, finalLen - 1) return answer End
Example
#include<iostream> using namespace std; int min(int x, int y, int z) { //find smallest among three numbers if(x < y) { if(x < z) return x; else return z; }else { if(y < z) return y; else return z; } } int editCount(string initStr , string finalStr, int initLen, intfinalLen) { if (initLen == 0) //when initial string is empty, add all characters of final string return finalLen; if (finalLen == 0) //when final string is empty, delete all characters from initial string return initLen; //when last character matches, recursively check previous characters if (initStr[initLen-1] == finalStr[finalLen-1]) return editCount(initStr, finalStr, initLen-1, finalLen-1); //if last character match is not found, check for insert, delete and update operations recursively return 1 + min ( editCount(initStr, finalStr, initLen, finalLen- 1), // insert editCount(initStr, finalStr, initLen-1, finalLen), // delete editCount(initStr, finalStr, initLen-1, finalLen-1) // update ); } int main() { string initStr; string finalStr; cout << "Enter the initial string: "; cin >> initStr; cout << "Enter the final string: "; cin >> finalStr; cout << "The number of changes required to convert " << initStr << " to " << finalStr; cout << " is " << editCount( initStr , finalStr, initStr.size(), finalStr.size()) << endl; }
Output
Enter the initial string: Programming Enter the final string: Programs The number of changes required to convert Programming to Programs is 4
- Related Articles
- Edit Distance in C++
- One Edit Distance in C++
- Check if edit distance between two strings is one in Python
- Program to find the minimum edit distance between two strings in C++
- Program to check two strings are 0 or 1 edit distance away or not in Python
- What is a distance-based outlier?\n
- Python – Remove Elements in K distance with N
- How to edit photos using Canva?
- Edit report generated from SAP Crystal Reports
- How can we edit a java program?
- Loop through array and edit string JavaScript
- Python Program to Edit objects inside tuple
- How to edit a comboBox in JavaFX?
- Delete and Edit items in Tkinter TreeView
- How to edit old designs in Canva?

Advertisements