Java Program to Search longest Repeating Sequence

In this program we will take string input,after taking string input we will try to find the sequences/series that are formed and will return longest string sequence among them

For Example:-
lets take String - abcxyoabcptrabc
In this example there is a sequence 'abc' which is repeated 3 times and its length is 3.
In this way we have to find the sequence and return it as an output.

Note: For Mobile user please view this topic on Desktop mode

Algorithm/Program Explanation

Main Method Explanation

  1. Start
  2. System.out.println(“Enter The String:”);//For taking String Input
  3. Take user Defined Inputs :
    • String string1=sc.nextLine();// we have take abcxyoabcptrabc as string input
  4. Define String string2=”” //for futher use
  5. Define variable length and initialize it.
    • length=string1.length // hence length =15
  6. Define two for loop
    • for(int i=0;i<length;i++)
    • for(j=i+1;j<length;j++)
    • string s3=longestprefix(string1.substring(i,length),string1.substring(j,length));
      • (abcxyoabcptrabc, bcxyoabcptrabc) will be given to longestprefix method
    • longestprefix execution-
      • String s1 contain-abcxyoabcptrabc
      • String s2 contain -bcxyoabcptrabc
      • int length = Math.min(s1.length(), s2.length());//
      • length=14 as s1=15 < s2=14
      • for (int i = 0; i < 14; i++)
      • if (s1.charAt(i) != s2.charAt(i))
        • s1-abc xyo abc ptr abc s2=bcx yoa bcp tra bc
        • here in 1st iteration a from s1 is comapred with b of s2 hence they are not equal
        • it return s1.substring(0, i) that a
        • now b from s1 is comapred with c of s2 as b !=c hence it return
        • it return s1.substring(0, i) that is ab
        • now c from s1 is compared with x of s2 as c !=x hence it return
        • it return s1.substring(0, i) that is abc
  7. repeat step 6 and 5 till length you will get you longest sequence.
  8. End

Program

import java.util.*;
import java.io.*;
public class longestsequence{
    public static String longestprefix(String s1, String s2) {
        int length = Math.min(s1.length(), s2.length());
        for (int i = 0; i < length; i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                return s1.substring(0, i);
            }
        }
        return s1.substring(0, length);
    }
    public static void main(String args[])  {
        System.out.println("Enter The String:");
        Scanner sc=new Scanner(System.in);
        String string1=sc.nextLine();
        String string2= "";
        int length=string1.length();
        for(int i=0;i<length;i++){
            for(int j=i+1;j<length;j++){
                String string3=longestprefix(string1.substring(i,length),string1.substring(j,length));
                 if(string3.length()>string2.length()){
                     string2=string3;

                 }
            }
             } System.out.println("Longest Sequence is:"+ string2);
            }
}
Output:
Enter The String:
abcxyoabcptrabc
Longest Sequence is:abc

Still if you didn’t understood the code mail me I will send you handwritten explanation for this code -infonoteshacker@gmail.com

Related Topics

Leave a Comment