Home » Program to Find the Longest Repeating Sequence in a String

Program to Find the Longest Repeating Sequence in a String

by Online Tutorials Library

Q. Program to find the longest repeating sequence in a string.

Explanation

In this program, we need to find the substring which has been repeated in the original string more than once.

a b d f a a b d f h

In the above string, the substring bdf is the longest sequence which has been repeated twice.

Algorithm

  1. Define a string and calculate its length.
  2. Define a function for the longest common prefix that is, it takes two strings as arguments and determines the longest group of characters common in between them.
  3. Using the same function, we will compare original string will all its substrings. Then, first for loop compare all the substrings with all the other substrings till we find the longest repeating sequence.
  4. Storing the longest string in the variable lrs if the length of x is greater than lrs.

Solution

Python

Output:

Longest repeating sequence: bdf 

C

You may also like