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