Re: [revisited] similar strings
Re: [revisited] similar strings
- Subject: Re: [revisited] similar strings
- From: Mark Lively <email@hidden>
- Date: Mon, 9 Jan 2006 11:37:08 -0500
On 1/9/06, Feat <email@hidden> wrote:
> In more general terms, how could I detect the longest substring shared by two strings?
>
> I tried to solve this using AppleScript handlers, and Lisp and C functions, but cannot find a simple solution.
>
> Is there a scripting addition that would ease that task?
>
I don't know of any and I can't think of a good way to tackle the
problem without a great deal of brute force.
set string1 to "asdagsadhwrrtyqweehafsshsdgawiuetesthsdgfaotyjhsdga"
set string2 to "thisisatest"
if (count string1) > (count string2) then
set bigstring to string1
set littlestring to string2
else
set bigstring to string2
set littlestring to string1
end if
set littleStringLength to count littlestring
set teststring to ""
set outlength to 0
set outstring to ""
set opcount to 0
repeat with i from 1 to littleStringLength
if littleStringLength - i < outlength then exit repeat
set j to littleStringLength
repeat
if (j - i + 1) < outlength then exit repeat
set teststring to text i through j of littlestring
log teststring
set opcount to opcount + 1
if bigstring contains teststring then exit repeat
set j to j - 1
end repeat
if (count teststring) > outlength then
set outstring to teststring
set outlength to count teststring
end if
end repeat
{outstring, outlength, opcount}
50 contains for those two strings
-Mark
It would be much faster in C or perl
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden