https://leetcode.com/problems/minimum-genetic-mutation
8글자짜리 유전자 서열 startGene 에서 endGene 까지 몇 번 걸려서 갔는지 (bank 에 있는 단어들 통해서)
class Solution {
public int minMutation(String startGene, String endGene, String[] bank) {
Set<String> bankSet = new HashSet<>(Arrays.asList(bank));
if (!bankSet.contains(endGene)) {
return -1;
}
Queue<String> queue = new ArrayDeque<>();
Set<String> visited = new HashSet<>();
char[] choices = {'A', 'C', 'G', 'T'};
queue.offer(startGene);
visited.add(startGene);
int mutations = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
String current = queue.poll();
if (current.equals(endGene)) {
return mutations;
}
for (int j = 0; j < 8; j++) {
for (char choice: choices) {
char[] chars = current.toCharArray();
chars[j] = choice;
String mutated = new String(chars);
if (bankSet.contains(mutated) && !visited.contains(mutated)) {
queue.offer(mutated);
visited.add(mutated);
}
}
}
}
mutations++;
}
return -1;
}
}