0433. Minimum Genetic Mutation

433. Minimum Genetic Mutation #

Problem #

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.
  3. You may assume start and end string is not the same.

Example 1:

start: "AACCGGTT"
end:   "AACCGGTA"
bank: ["AACCGGTA"]

return: 1

Example 2:

start: "AACCGGTT"
end:   "AAACGGTA"
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

return: 2

Example 3:

start: "AAAAACCC"
end:   "AACCCCCC"
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

return: 3

Problem Summary #

Now given 3 parameters — start, end, bank, which respectively represent the starting gene sequence, the target gene sequence, and the gene bank, find the minimum number of changes needed to transform the starting gene sequence into the target gene sequence. If the target transformation cannot be achieved, return -1.

Note:

  1. The starting gene sequence is valid by default, but it may not necessarily appear in the gene bank.
  2. All target gene sequences must be valid.
  3. Assume that the starting gene sequence and the target gene sequence are not the same.

Solution Ideas #

  • Given two strings start and end and a string array bank, ask for the minimum number of transformations needed to transform the start string into the end string. Each transformation must use a value from the bank string array.
  • This problem is exactly a remake of Problem 127; the solution idea and code are 99% the same. Similar problems also include Problem 126. This problem is simpler than both of them. There are 2 solutions, BFS and DFS. For the specific idea, see the solution to Problem 127.

Code #


package leetcode

// Solution 1 BFS
func minMutation(start string, end string, bank []string) int {
	wordMap, que, depth := getWordMap(bank, start), []string{start}, 0
	for len(que) > 0 {
		depth++
		qlen := len(que)
		for i := 0; i < qlen; i++ {
			word := que[0]
			que = que[1:]
			candidates := getCandidates433(word)
			for _, candidate := range candidates {
				if _, ok := wordMap[candidate]; ok {
					if candidate == end {
						return depth
					}
					delete(wordMap, candidate)
					que = append(que, candidate)
				}
			}
		}
	}
	return -1
}

func getCandidates433(word string) []string {
	var res []string
	for i := 0; i < 26; i++ {
		for j := 0; j < len(word); j++ {
			if word[j] != byte(int('A')+i) {
				res = append(res, word[:j]+string(int('A')+i)+word[j+1:])
			}
		}
	}
	return res
}

// Solution 2 DFS
func minMutation1(start string, end string, bank []string) int {
	endGene := convert(end)
	startGene := convert(start)
	m := make(map[uint32]struct{})
	for _, gene := range bank {
		m[convert(gene)] = struct{}{}
	}
	if _, ok := m[endGene]; !ok {
		return -1
	}
	if check(startGene ^ endGene) {
		return 1
	}
	delete(m, startGene)
	step := make(map[uint32]int)
	step[endGene] = 0
	return dfsMutation(startGene, m, step)
}

func dfsMutation(start uint32, m map[uint32]struct{}, step map[uint32]int) int {
	if v, ok := step[start]; ok {
		return v
	}
	c := -1
	step[start] = c
	for k := range m {
		if check(k ^ start) {
			next := dfsMutation(k, m, step)
			if next != -1 {
				if c == -1 || c > next {
					c = next + 1
				}
			}
		}
	}
	step[start] = c
	return c
}

func check(val uint32) bool {
	if val == 0 {
		return false
	}
	if val&(val-1) == 0 {
		return true
	}
	for val > 0 {
		if val == 3 {
			return true
		}
		if val&3 != 0 {
			return false
		}
		val >>= 2
	}
	return false
}

func convert(gene string) uint32 {
	var v uint32
	for _, c := range gene {
		v <<= 2
		switch c {
		case 'C':
			v |= 1
		case 'G':
			v |= 2
		case 'T':
			v |= 3
		}
	}
	return v
}


Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文