4
$\begingroup$

Is there a solution in Mathematica to the problem of finding the longest substring of a sequence that occurs at least twice.For example {1, 2, 3, 4, 5, 2, 3} has the solution {2,3} or "ABCDEDEFDEFA" has the solution "DEF".

$\endgroup$
3
$\begingroup$

Brute force:

{1, 2, 3, 4, 5, 2, 3} /. {___, Longest[n__], ___, n__, ___} :> {n}

{1,2}

Characters["ABCDEDEFDEFA"] /. {___, Longest[n__], ___, n__, ___} :> StringJoin[n]

"DEF"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.