Teaching Kids Programming: Videos on Data Structures and Algorithms
A gene string can be represented by an 8-character long string, with choices from ‘A’, ‘C’, ‘G’, and ‘T’. Suppose we need to investigate a mutation from a gene string startGene to a gene string endGene where one mutation is defined as one single character changed in the gene string. For example, “AACCGGTT” –> “AACCGGTA” is one mutation. There is also a gene bank bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string. Given the two gene strings startGene and endGene and the gene bank bank, return the minimum number of mutations needed to mutate from startGene to endGene. If there is no such a mutation, return -1.
Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Input: startGene = “AACCGGTT”, endGene = “AACCGGTA”, bank = [“AACCGGTA”]
Output: 1Example 2:
Input: startGene = “AACCGGTT”, endGene = “AAACGGTA”, bank = [“AACCGGTA”,”AACCGCTA”,”AAACGGTA”]
Output: 2Constraints:
0 <= bank.length <= 10
startGene.length == endGene.length == bank[i].length == 8
startGene, endGene, and bank[i] consist of only the characters [‘A’, ‘C’, ‘G’, ‘T’].
Minimum Genetic Mutation via Iterative Deepening Search Algorithm
Iterative Deepening Search Algorithm (IDSA) is a Iterative Depth Limit Search. The idea of IDS is to perform a Depth Limit Search (DLS) incrementally until we find the node which then is the optimal (shortest), or none found if we reach the max threshold.
The IDS has the O(H) space complexity which is the same as the Depth First Search. The H is the height of the search tree.
The DLS is similar to Depth First Search except that we terminate the search when the depth exceeds the current max threshold and it can be also implemented in both Recurive and Iterative manners.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class Solution: def minMutation(self, start: str, end: str, bank: List[str]) -> int: bank = set(bank) def dls(d, s, e, seen = set()): if s == e: return True if s in seen or d == 0: return False seen.add(s) for n in "ACGT": for i in range(len(s)): x = s[:i] + n + s[i + 1:] if x != s and x in bank and dls(d - 1, x, e, seen): return True seen.remove(s) return False d = 0 N = len(bank) while d <= N: if dls(d, start, end): return d d += 1 return -1 |
class Solution: def minMutation(self, start: str, end: str, bank: List[str]) -> int: bank = set(bank) def dls(d, s, e, seen = set()): if s == e: return True if s in seen or d == 0: return False seen.add(s) for n in "ACGT": for i in range(len(s)): x = s[:i] + n + s[i + 1:] if x != s and x in bank and dls(d - 1, x, e, seen): return True seen.remove(s) return False d = 0 N = len(bank) while d <= N: if dls(d, start, end): return d d += 1 return -1
Minimal Gene Mutation
- Teaching Kids Programming – Minimum Genetic Mutation via Iterative Deepening Search Algorithm
- Teaching Kids Programming – Minimum Genetic Mutation via Breadth First Search Algorithm
- Teaching Kids Programming – Minimum Genetic Mutation via Depth First Search Algorithm
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Teaching Kids Programming - Minimum Genetic Mutation via Recursive Depth First Search Algorithm
Next Post: Teaching Kids Programming - Compound Interests and Euler's number (Math Constant E)