find the length of the longest repeating sequence in the given string.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram



import java.util.Scanner; 

public class LengthOfLongestRepeatingSequence  
    /* Function to find the length of longest repeating sequence */
    public static int findLRSeq(String str) 
        int i=0,j=0,n=0;
        int[][] dp;
        n = str.length(); 
        dp = new int[n+1][n+1]; 
        for (i=1; i<=n; i++) 
            for (j=1; j<=n; j++) 
                /* If characters match and indexes are not same*/ 
                if (str.charAt(i-1) == str.charAt(j-1) && i!=j) 
                    dp[i][j] =  1 + dp[i-1][j-1];           
                /* If characters do not match */
                    dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]); 
        return dp[n][n]; 
    public static void main (String[] args)  
    String sen="";
    Scanner sc = new Scanner(;    
        System.out.println("Enter a string");   
        sen =;
        System.out.println("The length of the longest sequence that repeats itself:"+findLRSeq(sen)); 


Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *