## Friday, October 21, 2016

### LeetCode 433 - Minimum Genetic Mutation

https://leetcode.com/problems/minimum-genetic-mutation/
A gene string can be represented by an 8-character long string, with choices from "A","C","G","T".
Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.
For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.
Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.

NOTE: 1. Starting point is assumed to be valid, so it might not be included in the bank. 2. If multiple mutations are needed, all mutations during in the sequence must be valid. For example,
bank: "AACCGGTA"
start: "AACCGGTT"
end: "AACCGGTA"
return: 1

bank: "AACCGGTA", "AACCGCTA", "AAACGGTA"
start: "AACCGGTT"
end: "AAACGGTA"
return: 2
bank: "AAAACCCC", "AAACCCCC", "AACCCCCC"
start: "AAAAACCC"
end: "AACCCCCC"
return: 3

https://discuss.leetcode.com/topic/63954/java-bfs-solution
Explain: Every time we find a mismatch letter then check if it is in the bank, if so, then we add it to the queue for bfs traverse. "#" is used to mark end of each level. time complexity should be o(length of bank)
``````    public int minMutation(String start, String end, String[] bank) {
Set<String> set=new HashSet<String>();//\\
char[] chars={'A','C','G','T'};

if(!set.contains(end))return -1;

while(!q.isEmpty()) {
Container con=q.remove();
char[] str=con.word.toCharArray();

for(int i=0;i<str.length;i++) {
char tmpChar=str[i];
for(char c:chars) {
if(c==str[i]) continue;
str[i]=c;
String tmpStr=new String(str);
if(tmpStr.equals(end))
return con.steps+1;
if(set.contains(tmpStr))

}
str[i]=tmpChar;
}
}
return -1;
}
class Container {
String word;
int steps;
public Container(String word,int steps) {
this.word=word;
this.steps=steps;
}
}``````
https://discuss.leetcode.com/topic/65780/java-solution-using-bfs
``````    public int minMutation(String start, String end, String[] bank) {
if(start.equals(end)) return 0;

Set<String> bankSet = new HashSet<>();

char[] charSet = new char[]{'A', 'C', 'G', 'T'};

int level = 0;
Set<String> visited = new HashSet<>();
queue.offer(start);

while(!queue.isEmpty()) {
int size = queue.size();
while(size-- > 0) {
String curr = queue.poll();
if(curr.equals(end)) return level;

char[] currArray = curr.toCharArray();
for(int i = 0; i < currArray.length; i++) {
char old = currArray[i];
for(char c: charSet) {
currArray[i] = c;
String next = new String(currArray);
if(!visited.contains(next) && bankSet.contains(next)) {
queue.offer(next);
}
}
currArray[i] = old;
}
}
level++;
}
return -1;
}``````

def minMutation(self, start, end, bank): """ :type start: str :type end: str :type bank: List[str] :rtype: int """ def toNumber(gene): table = {v : i for i, v in enumerate('ATGC')} return sum([table[g] * 1 << (2 * i) for i, g in enumerate(gene)]) bank = set(map(toNumber, bank)) start = toNumber(start) end = toNumber(end) queue = [(start, 0)] viset = set([start]) while queue: gene, step = queue.pop(0) if gene == end: return step for x in range(8): for y in range(4): next = gene ^ (y << (x * 2)) if next in bank and next not in viset: viset.add(next) queue.append((next, step + 1)) return -1

1. I tried running your java code on leet but it exceeds memory limitations....

2. The code based on bfs has a bug. After /* q.add(new Container(tmpStr,con.steps+1)); */ In the if condition only remove tmpstr from set so as not to add same str more than once in the queue.

1. public class Solution {
public int minMutation(String start, String end, String[] bank) {
Set hs=new HashSet();
char ch[]={'A','C','G','T'};
for(String s:bank)
if(!hs.contains(end))
return -1;
q.offer(new Node(start,0));
while(!q.isEmpty()){
Node n=q.poll();
char str[]=n.word.toCharArray();
for(int i=0;i<str.length;i++){
char temp=str[i];
for(char c:ch){
if(c==str[i])
continue;
str[i]=c;
String tempstr=new String(str);
if(tempstr.compareTo(end)==0)
return n.steps+1;
if(hs.contains(tempstr)){
q.offer(new Node(tempstr,n.steps+1));
hs.remove(tempstr);
}
}
str[i]=temp;
}
}
return -1;
}
}
class Node{
String word;
int steps;
Node(String word, int steps){
this.word=word;
this.steps=steps;
}
}