Skip to content

Commit cbca6f6

Browse files
authored
Create 433.py
1 parent d21972e commit cbca6f6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

101-500/433.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def canMutation(self, w, d, c, q):
3+
l = ["A",'C','G','T']
4+
for i in range(len(w)):
5+
for j in l:
6+
if w[i] != j:
7+
v = w[:i] + j + w[i + 1:]
8+
if v in d and (d[v] == 0 or d[v] > c + 1):
9+
d[v] = c + 1
10+
q.append(v)
11+
12+
def minMutation(self, start, end, bank):
13+
"""
14+
:type start: str
15+
:type end: str
16+
:type bank: List[str]
17+
:rtype: int
18+
"""
19+
if end not in bank:
20+
return -1
21+
dic = {}
22+
count = 0
23+
for i in bank:
24+
dic[i] = count
25+
queue = [start]
26+
while len(queue) > 0:
27+
q2 = []
28+
for i in queue:
29+
self.canMutation(i, dic, count, q2)
30+
if dic[end] != 0:
31+
break
32+
queue = q2
33+
q2 = []
34+
count += 1
35+
# print dic
36+
if dic[end] == 0:
37+
return -1
38+
return dic[end]

0 commit comments

Comments
 (0)