-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatingSequence.java
More file actions
41 lines (37 loc) · 1.43 KB
/
repeatingSequence.java
File metadata and controls
41 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Java Program to find the longest repeating sequence in a string
// I/P : str = acbdfghybdf
// O/P : bdf
import java.util.Scanner;
public class repeatingSequence {
public static void main(String[] args) {
String sText, sMax = "", seq2;
Scanner input = new Scanner(System.in);
int iMax = 0, iCount = 0;
System.out.print("Enter the String Sequence: ");
sText = input.nextLine();
for(int steps=1; steps <= sText.length(); steps++){
for (int i = 0; i < sText.length(); i++) {
int count = 0;
if(i+steps>sText.length()){
break;
}
String check = sText.substring(i, i+steps);
for (int j = 0; j < sText.length(); j++){
if(j+steps>sText.length()){
break;
}
if(check.equals(sText.substring(j, j+steps))){
// System.out.println("working loop");
count++;
}
}
if(count >= iCount){
iCount = count;
sMax = check;
}
// System.out.print("Sequence: " + check + " = " + count + "\n");
}
}
System.out.println("Longest repeating sequence is " + sMax + ", (" + iCount + ")times.");
}
}