forked from anmol098/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editDistance.cpp
62 lines (55 loc) · 1.36 KB
/
editDistance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
*
* You have the following 3 operations permitted on a word:
*
* a) Insert a character
* b) Delete a character
* c) Replace a character
*
*/
#include <iostream>
#include <string>
using std::string;
int min( int a, int b, int c ) {
int minab = ( a < b ) ? a : b;
return ( minab < c ) ? minab:c;
}
int minEditDistance(string word1, string word2) {
int len1 = word1.size();
int len2 = word2.size();
int minCost;
int ** table = new int*[len1+1];
for ( int i = 0;i <= len1; ++i ) {
table[i] = new int[len2+1];
}
for (int i = 0; i <= len1; ++i) {
for (int j = 0; j <= len2; ++j ) {
if ( i == 0 ) {
table[i][j] = j;
}
else if ( j == 0 ) {
table[i][j] = i;
} else if ( word1[i-1] == word2[j-1]) {
table[i][j] = table[i-1][j-1];
} else {
table[i][j] = 1+ min(table[i-1][j-1], table[i][j-1], table[i-1][j]);
}
}
}
minCost = table[len1][len2];
for ( int i = 0; i <= len1; ++i ) {
delete table[i];
}
delete[] table;
return minCost;
}
int main() {
std::string word1, word2;
std::cout << "Enter word 1 : " ;
std::cin >> word1;
std::cout << "Enter word 2 : " ;
std::cin >> word2;
std::cout << "Min edit distance : " << minEditDistance(word1, word2) << std::endl;
return 0;
}