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
- Start
- System.out.println(“Enter The String:”);//For taking String Input
- Take user Defined Inputs :
- String string1=sc.nextLine();// we have take abcxyoabcptrabc as string input
- Define String string2=”” //for futher use
- Define variable length and initialize it.
- length=string1.length // hence length =15
- 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
- repeat step 6 and 5 till length you will get you longest sequence.
- 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
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output