Escolar Documentos
Profissional Documentos
Cultura Documentos
In this article, I will discuss my solution to the Genome Sequencing puzzle in Codingame. My solution
uses a brute forced approach.
Merge method
First thing’s first – merging two strings. This method returns the best possible merged value of 2
strings. This is it does by superimposing the 2 strings, and moving them apart every turn. As soon as
we have a match, we return the currently merged string.
public static String merge(String s1,String s2) {
if (s1.length()<s2.length()) {
String tmp = s2;
s2 = s1;
s1 = tmp;
}
if (s1.indexOf(s2)!=-1) return s1;
for (int i = s2.length()-1; i > 0; i--) {
String sub1 = s2.substring(0,i);
String sub2 = s2.substring(s2.length()-i);
if (s1.substring(0,i).equals(sub2)) {
String result = s2.substring(0,s2.length()-i)+s1;
return result;
}
if (s1.substring(s1.length()-i).equals(sub1)) {
String result = s1+s2.substring(i);
return result;
}
}
return "";
}
if (s1.length()<s2.length()) {
String tmp = s2;
s2 = s1;
s1 = tmp;
}
In our method, s2 is the smaller string and s1 the larger, so we first do this quick swap.
if (s1.substring(0,i).equals(sub2)) {
String result = s2.substring(0,s2.length()-i)+s1;
return result;
}
If the front of s1 matches with the back of s2, then this is the best possible merge.
if (s1.substring(s1.length()-i).equals(sub1)) {
String result = s1+s2.substring(i);
return result;
}
If the back of s1 matches with the front of s2, then this is the best possible merge.
findSmall method
given the current string, we recursively try to find the shortest string that can be made by combining
the remaining DNA strands.
public static void findSmall(String LAST,int I,int J,Vector<String> strips,int N)
{
if (N==1) {
shortest=Math.min(LAST.length(),shortest);
return;
}
strips.set(I,LAST);
strips.removeElementAt(J);
if (I>J) I--;
for (int i = 0; i < N; i++) {
if (i==I) continue;
String merged = merge(LAST,strips.elementAt(i));
if (!merged.equals("")) {
Vector<String> check = new Vector<String>();
check.addAll(0,strips);
findSmall(merged,I,i,check,N-1);
}
else {
Vector<String> check = new Vector<String>();
check.addAll(0,strips);
findSmall(LAST+strips.elementAt(i),I,i,check,N-1);
findSmall(strips.elementAt(i)+LAST,I,i,check,N-1);
}
}
}
else {
Vector<String> check = new Vector<String>();
check.addAll(0,strips);
findSmall(LAST+strips.elementAt(i),I,i,check,N-1);
findSmall(strips.elementAt(i)+LAST,I,i,check,N-1);
}
If no merging is possible (O_O), we can try 2 combinations –
(I) LAST+strips.get(i)
(II) strips.get(i)+LAST.
getDNA method
this method is called by main, to return the best match. It brute forces every 2 pairs of strands to
start with, and then call findSmall().
public static void getDNA(Vector<String> strips,int N) {
for (int i = 0; i < N-1; i++) {
for (int j = i+1; j < N; j++) {
String merged = merge(strips.elementAt(i),strips.elementAt(j));
if (!merged.equals("")) {
Vector<String> check = new Vector<String>();
check.addAll(0,strips);
findSmall(merged,i,j,check,N-1);
}
}
}
}
I feel this method is pretty self explanatory. It tries every possible pair of first 2 strands, then calls
merge method, if there is any use merging the two strands.
Note: see that getDNA does not provide for a test case where no mergings are possible. This is why
in the main method we set smallest as the sum of the length of all the strings.
Conclusion
Yaay! We are done. A simple solution to the genome sequencing puzzle, which might seem tough at
first site. In conclusion I say that the code shared here is in Java. The main method and import
statements are missing. I believe the reader can do this. (strips passed to getDNA is the collection of
the DNA strands).