mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-15 09:57:09 +00:00
Time: 0 ms (100%), Space: 12.5 MB (51.13%) - LeetHub
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
class Solution(object):
|
||||||
|
def minMutation(self, startGene, endGene, bank):
|
||||||
|
"""
|
||||||
|
:type startGene: str
|
||||||
|
:type endGene: str
|
||||||
|
:type bank: List[str]
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
|
||||||
|
def isMutation(g1, g2):
|
||||||
|
numDiff = 0
|
||||||
|
for i in range(len(g1)):
|
||||||
|
if g1[i] != g2[i]:
|
||||||
|
numDiff += 1
|
||||||
|
if numDiff > 1:
|
||||||
|
return False
|
||||||
|
return numDiff == 1
|
||||||
|
|
||||||
|
if endGene not in bank:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
queue = [(startGene, 0)]
|
||||||
|
seen = [False] * len(bank)
|
||||||
|
|
||||||
|
while len(queue) > 0:
|
||||||
|
currentGene, currentDistance = queue.pop(0)
|
||||||
|
|
||||||
|
for i, gene in enumerate(bank):
|
||||||
|
if seen[i]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if isMutation(currentGene, gene):
|
||||||
|
if gene == endGene:
|
||||||
|
return currentDistance + 1
|
||||||
|
|
||||||
|
queue.append((gene, currentDistance + 1))
|
||||||
|
seen[i] = True
|
||||||
|
|
||||||
|
return -1
|
||||||
Reference in New Issue
Block a user