Minimum Genetic Mutation
LeetCode 433 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
BFS over one-letter mutations in the bank.
ನಾನು Minimum Genetic Mutation (LeetCode 433) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — State-Space BFS ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Each gene is a node; a valid one-letter change to a gene in the bank is an edge. The fewest mutations is the shortest path, found with BFS.
Approach
- 1
Queue (start, 0).
- 2
Pop; if it's the end gene, return steps.
- 3
Try every position × every letter; enqueue unvisited bank genes.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ
BFSPythonO(B·L) time
class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
q = deque([(startGene,0)])
visited = set()
while q:
word,steps = q.popleft()
if word==endGene:
return steps
for i in range(len(word)):
for char in ("A","C","G","T"):
new_word = word[:i]+char+word[i+1:]
if new_word in bank and new_word not in visited:
visited.add(new_word)
q.append((new_word,steps+1))
return -1
Minimum Genetic Mutation — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Minimum Genetic Mutation ಹಿಂದಿನ intuition ಏನು?
Each gene is a node; a valid one-letter change to a gene in the bank is an edge. The fewest mutations is the shortest path, found with BFS.
Minimum Genetic Mutation (LeetCode 433) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Queue (start, 0). 2. Pop; if it's the end gene, return steps. 3. Try every position × every letter; enqueue unvisited bank genes.
Minimum Genetic Mutation ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(B · L · 4). Space: O(B). (Accepted approach: BFS.)
Minimum Genetic Mutation ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Use a set for the bank — list membership is O(B).