Hi Monks!
I am trying to implement Edit Distance Interview problem in Perl, but seems like there is some issue. Below is my code
sub editDistance{
my($str1, $str2, $len1, $len2) = @_;
if ($len1 ==0){
return $len2;
}
if ($len2 ==0){
return $len1;
}
if(substr($str1, -1) eq substr($str2,-1)){
return editDistance($str1,$str2,$len1-1,$len2-1);
}
my @distance = ();
push @distance, editDistance($str1, $str2, $len1, $len2-1); #inser
+t
push @distance, editDistance($str1, $str2, $len1-1, $len2); #remov
+e
push @distance, editDistance($str1, $str2, $len1-1, $len2-1); #rep
+lace
my $min = min @distance;
return 1 + $min;
}
my $str1 ="Sundayyyy";
my $str2 = "Saturday";
print editDistance($str1, $str2, length($str1), length($str2))
Where did I go wrong ?
http://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.