how to write a method in Java The first part of your DNA ana
Solution
static String solution(String DNA1, String DNA2) {
// Calculated length of DNAs as
// it will be useful at multiple places.
int DNA1Length = DNA1.length();
int DNA2Length = DNA2.length();
/*
* Handled corner cases
* Ex. if DNA1 = \"\"
* And DNA2 = \"ATATATATA\"
* Then return \"ATATATATA\"
*/
if (DNA1Length == 0) {
return DNA2;
} else if (DNA2Length == 0) {
return DNA1;
} else if (DNA1.equals(DNA2)) {
return DNA1;
}
/*
* This for loop will try to find
* longest terminating overlap between DNAs.
* It will start with minimum length of both DNAs
* and keep decrementing one.
* It will check if DNA1 ends with any substring of DNA2.
*
* if it has any overlap then,
* it will return DNA1 + remaining part of DNA2.
*
* else it will return concatenation of DNA1 and DNA2.
*/
for (int i = Math.min(DNA1Length, DNA2Length); i > 0 ; i--) {
if(DNA1.endsWith(DNA2.substring(0, i)) ) {
return DNA1 + DNA2.substring(i);
}
}
return DNA1 + DNA2;
}
