I realize you said "efficiently", but if you ever miss that old Bubble Sort 
feeling...

$str1:="This is my dog"
$str2:="My dog does not have fleas"

$str1:="The quick brown fox jumps over the lazy dog"
$str2:="The quick black fox leaps over the laziest dog"

ARRAY TEXT($aMatch;0)
getCommon ($str1;$str2;->$aMatch)

SORT ARRAY($aMatch;<)


  // ----------------------------------------------------
  // Method: getCommon - needs some optimization
  // - 
  // INPUT1: Text
  // INPUT2: Text
  // INPUT3: Pointer - to text array
  // OUTPUT: 
  // ----------------------------------------------------
$str1:=$1
$str2:=$2

If (Length($str1)>Length($str2))
        $longer:=$str1
        $shorter:=$str2
Else 
        $longer:=$str2
        $shorter:=$str1
End if 

$shortLen:=Length($shorter)
$longLen:=Length($longer)
$maxLen:=$shortLen*2+$longLen
$loop:=1
$buildText:=""

Repeat 
        ARRAY TEXT($aMatch;0)
        For ($i;1;$loop)
                $str1:=Substring($shorter;$shortLen-$i)
                $str2:=Substring($longer;1;$loop+1)

                For ($j;1;Length($str1))
                        If ($str1[[$j]]=$str2[[$j]])
                                $buildText:=$buildText+$str1[[$j]]
                        Else 
                                APPEND TO ARRAY($aMatch;$buildText)
                                $buildText:=" "
                        End if 
                End for 

        End for 
        $loop:=$loop+1
Until ($loop>$maxLen)
 
COPY ARRAY($aMatch;$3->)


Keith - CDI

> On Aug 22, 2019, at 3:12 PM, Chip Scheide via 4D_Tech <4d_tech@lists.4d.com> 
> wrote:
> 
> Given 2 strings, 
> I want to find, and return, the longest substring which is the same in 
> both, regardless where in either string the longest substring starts.
> 
> ex: 
> 1- This is my dog
> 2- My dog does not have fleas
> longest common string is 'my dog'
> 
> how to go about this, efficiently?
> I am assuming that there is regex black magic that would do this.
>  

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to