word_ladder
word_ladder.py 源码
from typing import List
# 单词接龙
class Solution:
# 广度优先搜索的思路
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordSet = set(wordList)
if endWord not in wordSet:
return 0
queue, res = [beginWord], 0
while len(queue) > 0:
n = len(queue)
for i in range(n):
cur = queue[0]
queue = queue[1:]
if cur == endWord:
return res + 1
for i in range(len(cur)):
for c in range(97, 97 + 26):
modStr = cur[:i] + chr(c) + cur[i + 1:]
if modStr in wordSet:
queue.append(modStr)
wordSet.remove(modStr)
res += 1
return 0
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦