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).