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