BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find the longest repeating sequence in a string

Last Updated: July 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write a java program to find the longest repeating sequence in a string. For example, if the given string is “abracadabra” then the longest repeating sequence in this string would be “abra”.

Highlighting longest re-occurring sequence in the string: abrakadabra.

Program to find the longest reoccurring sequence in a given String

In this program, we have a user defined method longestCP() which determines which substring has the longest common prefix. In the main() method of the program, we are running a nested for loop and passing the substrings of the given string to the method we created, the method then returns the largest common prefix.

At the end of the loop, the largest common prefix is compared with the previously found largest repeating sequence and the largest between these two replaces the value of longestRS.

public class JavaExample {
  //This method returns the longest common prefix
  //This will be called to determine which substring has
  //the largest common prefix.
  public static String longestCP(String s, String t){
    int n = Math.min(s.length(),t.length());
    for(int i = 0; i < n; i++){
      if(s.charAt(i) != t.charAt(i)){
        return s.substring(0,i);
      }
    }
    return s.substring(0,n);
  }

  public static void main(String[] args) {
    String str = "abracadabra";
    String longestRS="";
    int len = str.length();
    for(int i = 0; i < len; i++){
      for(int j = i+1; j < len; j++){ 
          String s =
           longestCP(str.substring(i,len),str.substring(j,len)); 
          if(s.length() > longestRS.length())
           longestRS=s;
      }
    }
    System.out.println("Longest repeating sequence" +
            " in the given string is: "+longestRS);
  }
}

Output:
Java Program to find the longest repeating sequence in a string

Related Java Examples

  1. Java Program to find longest substring without repeating characters
  2. Java Program to find all subsets of a String
  3. Java Program to divide a String in ‘n’ equal parts
  4. Java Program to check if two strings are anagram or not
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to find all subsets of a string
  2. Java Program to Calculate average using Array
  3. Java Program to Check two Strings are anagram or not
  4. Java Program to Swap two numbers using Bitwise XOR Operator
  5. Java Program to print the duplicate elements of an array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap