Android Code Snippet measuring the difference between two strings - good for approximat searchsearch

Subame:LevenshteinDistance
Description : this sub compare two strinss and return int as the result , more information can be found in :http://en.wikipedia.org/wiki/Levenshtein_distance
Dependencis: none

B4X:
Sub LevenshteinDistance(s As String, t As String) As Int
' d Is a table with m+1 rows AND n+1 columns
Dim m As Int=s.Length
Dim n As Int=t.Length
Dim d(m+2,n+2) As Int

For i=0 To m
d(i, 0) = i ' deletion
Next
For j=0 To n
d(0, j) = j ' insertion
Next

For j=1 To n
For i=1 To m
If s.CharAt(i-1) = t.CharAt(j-1) Then
d(i, j) = d(i-1, j-1)
Else
d(i, j) = minimum(d(i-1, j) + 1,d(i, j-1) + 1,d(i-1, j-1) + 1)
End If
Next
Next
Return d(m,n)
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Thanx for sharing. But could you be so kind and edit your post and put the right "commands" in this post. See here.
SubName:

Description: A brief outline of the purpose of the snippet

Code (in code block)

Dependencies: A list of additional libraries that the code needs to work.

Tags: <list of keywords>
 
Top