To print longest pattern sequence
Problem: Write a program to print longest pattern sequence. e.g. Input String str="123424861234567"; Patterns are "1234","2486","1234567" with common difference 1,2,1 respectively.So being the longest third pattern i.e. "1234567" is the output.
package assignment1;
/**
* @author snlkjha
*/
public class Str {
public static void lognest_str_pattern(char s[]){
int count1[]=new int[5],count2[]=new int[5],t=0,max=0,i,j; // Variable declaration
for(i=0;i<s.length;i=i+j+1){
j=0;
while((i+j+1)<s.length && (s[i+j+1]-'0')-(s[i+j]-'0')==((s[i+1]-'0')-(s[i]-'0'))) j++;
count2[t]=i+j+1;
count1[t]=j+1;
max=count1[max]<count1[t]?t:max; // finding index of maximum elelment in the array
t++;
}
System.out.print("Output :");
for(i=(count2[max]-count1[max]);i<count2[max];i++) System.out.print(s[i]); // printing longest pattern
System.out.println();
}
public static void main(String[] args){
String x="1234512345987654321";
System.out.println("Input :"+x);
char s[]=x.toCharArray();
lognest_str_pattern(s);
}
}
Input :1234512345987654321
Output :987654321