DNA Mutation Java

prashant kumar
2 min readMar 13, 2021

There is a DNA Strand having values as A , T , C , G.
All combinations are present in the the file.

Write a method which takes starting mutation string , ending mutation string and string bank and calculates the minimum mutation distance required. But the condition is that either of the start or end must be present in the bank.

Input:
AATTGGCC is starting and TTTTGGCA is ending then mutation distance will be 3.
AATTGGCC — TATTGGCC — TTTTGGCC — TTTTGGCA as it takes three mutation for start to reach the end string and for this , all intermediate string and final string must be present in the bank.

Solution:

If the starting strand is considered as a node in a tree then we generate every possible mutation of that strand and continue doing so until the end result is achieved. The answer will be the number of levels created for us to reach the resultant DNA strand.

This can be achieved with BFS where the number of levels it takes to reach the end string will be the answer

Code:

import java.util.LinkedList;
import java.util.Queue;

public class DNAMutation {
public static void main(String[] args) {
String[] bank = {"A", "T", "C", "G"};
String s1 = "AATTGGCC";
String s2 = "TTTTGGCA";
System.out.println(mutationDistance(s1, s2, bank));
}

private static int mutationDistance(String start, String end, String[] bank) {
Queue<String> q = new LinkedList<>();
q.add(start);
int mutationDistance = 0;
while(!q.isEmpty()) {
int n = q.size();
for (int x=0; x<n; x++) { // to ensure level by level traversal
String temp = q.poll();
// when the mutated string matches the end
if (end.equals(temp)) {
return mutationDistance;
}
// For every character, insert into queue, every possible mutation.
for (int i = 0; i < temp.length(); i++) {
for (String s : bank) {
StringBuilder sb = new StringBuilder(temp);
if (s.charAt(0) != temp.charAt(i)) {
sb.setCharAt(i, s.charAt(0));
q.add(sb.toString());
}
}
}
}
// Increase the mutation distance if all the mutations have been inserted in the queue
mutationDistance++;
}
return mutationDistance;
}
}

Output:

3

--

--