My question is to find the longest DNA sub-sequence that appears at least twice. The input is only one DNA string, NOT TWO strings as other LCS programs.
I have done my 4th program and it seems to be working and efficient (computes 10k strings in 4 seconds). But I don't know if this is 100 % correct. This is a really important project for me and I have really tried to make it.
Kindly provide your suggestions regarding my logic and program implementation in Java (I don't know advanced program techniques involving stacks, queues and linked lists in Java).
Logic:
- Enter string;
- Make new strings from the original string - start from beginning-- at each step increasing by one letter;
- Put these strings in an array;
- Sort the array in alphabetical order;
- Compare the prefixes between each string in the sorted array --looking for similar substrings ;
- Print the longest one(s).
import java.util.*;
/**
*
* @author Pavin
*/
public class dna3 {
public static String check(String a,String b)
{
String f="";
if (a.length()<b.length())
{
String h="";
// String f ="";
int r=a.length();
int t=b.length();
// }
String c="";
// String f ="";
if ((a==null)||(b==null))
{
c="";
}
else
{
for (int i=0;i<a.length();i++)
{
//for (int j=0;j<b.length();j++)
{
char d=a.charAt(i);
char e=b.charAt(i);
if (d==e)
{
c=String.valueOf(e);
f=f+c;
}
if (d!=e)
{
break;
}
}
}
}
}
if (a.length()>b.length())
{
String h="";
// String f ="";
int r=a.length();
int t=b.length();
// }
String c="";
// String f ="";
if ((a==null)||(b==null))
{
c="";
}
else
{
for (int i=0;i<b.length();i++)
{
//for (int j=0;j<b.length();j++)
{
char d=a.charAt(i);
char e=b.charAt(i);
if (d==e)
{
c=String.valueOf(e);
f=f+c;
}
if (d!=e)
{
break;
}
}
}
}
}
return f;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter DNA string");
String a=sc.nextLine();
String c;
String d;
int b=a.length();
String arr[]=new String [b+1];
int i;
for (i=0;i<b;i++)
{
c=a.substring(i,b);
arr[i]=c;
}
/* System.out.println("Before sort>>> ");
for (i=0;i<b;i++)
{
System.out.println(arr[i]);
}*/
String temp;
for(int x=1;x<b;x++)
{
for(int y=0;y<b-x;y++)
{
if(arr[y].compareTo(arr[y+1])>0)
{
temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
/* System.out.println("After sort>>> ");
for(i=0;i<b;i++){
System.out.println(arr[i]);
} */
System.out.println("Longest substring with frequency equal to two or more >>> ");
String v1;
String v2;
String v3;
// dnabxcfnabionabxuinab
int w=1;
for (i=0;i<b-1;i++)
{
v1= arr[i];
v2=arr[i+1];
v3=check(v1,v2);
int q=v3.length();
if (q>=w)
{
w=q;
}
}
String v4="";
for (i=0;i<b-1;i++)
{
v1= arr[i];
v2=arr[i+1];
v3=check(v1,v2);
int s=v3.length();
if ((s==w)&&(v4.compareTo(v3)!=0))
{
v4=v3;
System.out.println(v3);
}
}
// TODO code application logic here
}
}
ababa
the valueaba
or justab
orba
? \$\endgroup\$ – rolfl Nov 20 '13 at 20:19(.+).*\1
, so if you could do it in P time, you could also solve regex with back-references in P time, which is known to be NP-hard. Honestly, I can't read this code, please use real variable names, so I can't really analyze its complexity, but if it runs in polynomial time, and the problems are equivalent, there's some input for which it will fail. \$\endgroup\$ – sqykly Nov 23 '13 at 20:56