Tensorum
YouTubeसीखना शुरू करें
LeetCode 433 · State-Space BFS

Minimum Genetic Mutation

LeetCode 433 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

BFS over one-letter mutations in the bank.

मध्यमHash TableStringBreadth-First SearchBidirectional Search

मैंने 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. 1

    Queue (start, 0).

  2. 2

    Pop; if it's the end gene, return steps.

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