I am trying to generate the subsequences of two input string but my code is taking long to compile.Just need advice for optimizing the given code.Below is the code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
class Subsequences {
public static void combinations(String suffix,String prefix,List seq){
if(prefix.length()<0)return;
seq.add(suffix);
for(int i=0;i<prefix.length();i++)
combinations(suffix+prefix.charAt(i),prefix.substring(i+1,prefix.length()),seq);
}
public static void main(String args[] ) throws Exception {
List seq1=new ArrayList();
List seq2=new ArrayList();
boolean flag=false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
for (int i = 0; i < N; i++) {
combinations("",br.readLine(),seq1);
combinations("",br.readLine(),seq2,flag,seq1);
if(flag){
System.out.println("Yes");
}else{
System.out.println("No");
}
seq1.retainAll(seq2);
if(seq1.size()>1){
System.out.println("Yes");
}else{
System.out.println("No");
}
seq1.clear();
seq2.clear();
}
System.out.println("Hello World!");
}
}
Herein I am generating all the substrings of a string using recursion and storing all of these into arraylist.Then using retainAll I compare any common element between then.
Aucun commentaire:
Enregistrer un commentaire