How to resolve the algorithm Levenshtein distance step by step in the MiniScript programming language
How to resolve the algorithm Levenshtein distance step by step in the MiniScript programming language
Table of Contents
Problem Statement
In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
The Levenshtein distance between "rosettacode", "raisethysword" is 8. The distance between two strings is same as that when both strings are reversed.
Implements a Levenshtein distance function, or uses a library function, to show the Levenshtein distance between "kitten" and "sitting".
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Levenshtein distance step by step in the MiniScript programming language
Source code in the miniscript programming language
import "stringUtil"
print "kitten".editDistance("sitting")
string.editDistance = function(s2)
n = self.len
m = s2.len
if n == 0 then return m
if m == 0 then return n
s1chars = self.split("")
s2chars = s2.split("")
d = range(0, m)
lastCost = 0
for i in range(1, n)
s1char = s1chars[i-1]
lastCost = i
jMinus1 = 0
for j in range(1, m)
if s1char == s2chars[jMinus1] then cost = 0 else cost = 1
// set nextCost to the minimum of the following three possibilities:
a = d[j] + 1
b = lastCost + 1
c = cost + d[jMinus1]
if a < b then
if c < a then nextCost = c else nextCost = a
else
if c < b then nextCost = c else nextCost = b
end if
d[jMinus1] = lastCost
lastCost = nextCost
jMinus1 = j
end for
d[m] = lastCost
end for
return nextCost
end function
print "kitten".editDistance("sitting")
You may also check:How to resolve the algorithm A+B step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Hilbert curve step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the Ring programming language
You may also check:How to resolve the algorithm Mouse position step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the REXX programming language