Consequtive letter matches...please help

A

almurph

Hi,

Hope you can help me. This is a trick one - at least I think
so. I
have 2 strings. I have to calculate the "score" of the strings. Score
is determined by matching patterns of letters within the strings.
Essentially it is the sum of the consequtive letter matches.


Here are the rules of score:


1. Start at the end of the shorter string and and add up the amount of
letters found in the same order in the longer string. Continue for all
letters in the shorter string
2. I think that you may have to work from the end of the longer string
also
3. If strings are equal - pick either, note that the answer should be
the same regardless of which strings is compared against which.


As you can see the maximum value of score could be the length
of the
shortest string (in this case the shorter string is identical to the
longer, like a subset)


Simple Worked Example:


String1: "My name is John"
String2: "My name is Johnson"


Score = 12 (for "name isJohn") + 3 (for "My") = 15


Can anyone help me with coding this algorithm please?
Comments/sample-code greatly appreciated. Is this a well known string
matching algorithm?


Thank you,
Al.
 
G

Guest

I am not sure that I understand the rule. You say it is "add up the amount of
letters found in the same order in the longer string. Continue for all
letters in the shorter string", but that is not what you do in your example.
There the score is the length of the largest substring (in the shorter of
the 2 comparison strings) found in the other string. In either case, it is
not a difficult algorithm to program. Think of using a couple of nested For
loops and using the InStr function.
 
P

Phill W.

1. Start at the end of the shorter string and and add up
the amount of letters found in the same order in the longer string.
Continue for all letters in the shorter string

Now, do you mean the same "order", or the same actual characters, in the
same position that they appear in the original string?
To do the latter, you'd have something like

Function Score( _
ByVal s1 as String _
, ByVal s2 as String _
) as Integer

Dim iSub as Integer

For iSub = 1 To Len( s1 )
If Mid$( s1, 1 ) <> " " Then
If Mid$( s1, 1 ) <> Mid$( s2, 1 ) Then
Score = ( iSub - 1 )
Exit Function
End If
End If
Next

Score = Len( Replace( s1, " ", "" ) )
End Function

The above will count the matching /letters/, ignoring spaces.
Is this a well known string matching algorithm?

I've never come across it before... ;-)

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

High Score 1
Help with SQL string construction 3
RegEx Either Or 8
FileSystem.FindInFiles 10
Subtle String Question 5
Regular Expression Mystery 1
string help 3
Weird problem with connection strings 3

Top