671. Gene Mutation Steps
A sequencing lab stores every gene as a short string over the letters A, C, G and T. One mutation replaces exactly one letter of a gene with a different letter. A mutation is only allowed when the gene it produces is listed in the lab's approved catalogue bank; the gene you begin with does not have to be listed.
Return the smallest number of mutations that turns start into end, where every gene created along the way (including end) must appear in bank. If this cannot be done return -1. If start and end are already equal the answer is 0. The catalogue may contain repeated entries. Aim for O(B * L) time and space, where B is the catalogue size and L the gene length.
Example 1
- Input:
- start = "GATTC"end = "GCTAC"bank = ["GCTTC","GCTAC","GATAC"]
- Output:
- 2
- Explanation:
Mutate position 2 (A to C) to get GCTTC, then position 4 (T to A) to get GCTAC; both are in the bank, and no single mutation reaches the target, so the answer is 2.
Example 2
- Input:
- start = "CCCC", end = "CCGG", bank = ["CGCC","CCGG"]
- Output:
- -1
- Explanation:
CCCC can become CGCC, but CGCC differs from CCGG in three positions and no other bank gene helps, so the target cannot be reached.
Example 3
- Input:
- start = "TTAA", end = "TTAA", bank = []
- Output:
- 0
- Explanation:
The two genes are identical, so no mutation is needed.
Constraints
1 ≤ L ≤ 10, where L is the length of start, end and every string in bank
0 ≤ bank.length ≤ 20000
All strings contain only the letters A, C, G and T
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(B * L)
- Space
- O(B * L)