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
LeetCode లో తెరవండి ↗

నేను 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).