Alex Rivera | Logout

How do I find the largest sequence in a string that is repeated at least once?

Asked 2012-08-07T20:30:49.020
9

Trying to solve the following problem:

Given a string of arbitrary length, find the longest substring that occurs more than one time within the string, with no overlaps.

For example, if the input string was ABCABCAB, the correct output would be ABC. You couldn't say ABCAB, because that only occurs twice where the two substrings overlap, which is not allowed.

Is there any way to solve this reasonably quickly for strings containing a few thousand characters?

(And before anyone asks, this is not homework. I'm looking at ways to optimize the rendering of Lindenmayer fractals, because they tend to take excessive amounts of time to draw at high iteration levels with a naive turtle graphics system.)

Edit
Report

1 Answer

2

it looks like a suffix tree problem. Create the suffix tree, then find the biggest compressed branch with more than one child (occurs more than once in the original string). The number of letters in that compressed branch should be the size of the biggest subsequence.

i found something similar here: http://www.coderanch.com/t/370396/java/java/Algorithm-wanted-longest-repeating-substring

Looks like it can be done in O(n).

answered 2012-08-07T20:52:15.103

Your Answer