跳到主要内容

Word Ladder

描述

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  • Only one letter can be changed at a time
  • Each intermediate word must exist in the dictionary

For example, Given:

start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

分析

求最短路径,用广搜。

单队列

# Word Ladder
# 时间复杂度O(n*m),空间复杂度O(n)
from collections import deque
from typing import List, Set

class State:
def __init__(self, word: str, level: int):
self.word = word
self.level = level

def __hash__(self):
return hash(self.word)

def __eq__(self, other):
if self is other:
return True
if not isinstance(other, State):
return False
return self.word == other.word

def ladderLength(beginWord: str, endWord: str, wordList: List[str]) -> int:
q = deque()
visited = set()
word_dict = set(wordList)

def state_is_valid(s: State) -> bool:
return s.word in word_dict

def state_is_target(s: State) -> bool:
return s.word == endWord

def state_extend(s: State) -> List[State]:
result = []
array = list(s.word)
for i in range(len(array)):
old = array[i]
for c in range(ord('a'), ord('z') + 1):
c = chr(c)
# 防止同字母替换
if c == array[i]:
continue

array[i] = c
new_state = State(''.join(array), s.level + 1)

if state_is_valid(new_state):
result.append(new_state)
array[i] = old # 恢复该单词

return result

start_state = State(beginWord, 0)
visited.add(start_state)
q.append(start_state)

while q:
state = q.popleft()

if state_is_target(state):
return state.level + 1

new_states = state_extend(state)
for new_state in new_states:
if new_state not in visited:
visited.add(new_state)
q.append(new_state)

return 0

相关题目