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$

1 Answer 1

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 and acknowledge you have read our privacy policy.

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