VB.NET

2D Array

For

Module Module1
Sub Main()
Console.WriteLine(LevenshteinDistance(*"aunt"*, *"ant"*))
Console.WriteLine(LevenshteinDistance(*"Sam"*, *"Samantha"*))
Console.WriteLine(LevenshteinDistance(*"flomax"*, *"volmax"*))
End Sub
''' <summary>
''' Compute LevenshteinDistance.
''' </summary>
Public Function LevenshteinDistance(ByVal s As String,
ByVal t As String) As Integer
Dim n As Integer = s.Length
Dim m As Integer = t.Length
Dim d(n + 1, m + 1) As Integer
If n = 0 Then
Return m
End If
If m = 0 Then
Return n
End If
Dim i As Integer
Dim j As Integer
For i = 0 To n
d(i, 0) = i
Next
For j = 0 To m
d(0, j) = j
Next
For i = 1 To n
For j = 1 To m
Dim cost As Integer
If t(j - 1) = s(i - 1) Then
cost = 0
Else
cost = 1
End If
d(i, j) = Math.Min(Math.Min(d(i - 1, j) + 1, d(i, j - 1) + 1),
d(i - 1, j - 1) + cost)
Next
Next
Return d(n, m)
End Function
End Module1
5
3

Dictionary

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Jan 24, 2022 (edit).