-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEditDistance.m
38 lines (37 loc) · 1.06 KB
/
EditDistance.m
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
function [V,v] = EditDistance(string1,string2)
% Edit Distance is a standard Dynamic Programming problem. Given two strings s1 and s2, the edit distance between s1 and s2 is the minimum number of operations required to convert string s1 to s2. The following operations are typically used:
% Replacing one character of string by another character.
% Deleting a character from string
% Adding a character to string
% Example:
% s1='article'
% s2='ardipo'
% EditDistance(s1,s2)
% > 4
% you need to do 4 actions to convert s1 to s2
% replace(t,d) , replace(c,p) , replace(l,o) , delete(e)
% using the other output, you can see the matrix solution to this problem
%
%
% by : Reza Ahmadzadeh ([email protected] - [email protected])
% 14-11-2012
m=length(string1);
n=length(string2);
v=zeros(m+1,n+1);
for i=1:1:m
v(i+1,1)=i;
end
for j=1:1:n
v(1,j+1)=j;
end
for i=1:m
for j=1:n
if (string1(i) == string2(j))
v(i+1,j+1)=v(i,j);
else
v(i+1,j+1)=1+min(min(v(i+1,j),v(i,j+1)),v(i,j));
end
end
end
V=v(m+1,n+1);
end