forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_ladder.py
More file actions
79 lines (61 loc) · 2.19 KB
/
word_ladder.py
File metadata and controls
79 lines (61 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Word Ladder (Bidirectional BFS)
Given two words and a dictionary, find the length of the shortest
transformation sequence where only one letter changes at each step and
every intermediate word must exist in the dictionary.
Reference: https://leetcode.com/problems/word-ladder/
Complexity:
Time: O(N * L^2) where N = size of word list, L = word length
Space: O(N * L)
"""
from __future__ import annotations
from collections.abc import Iterator
def ladder_length(begin_word: str, end_word: str, word_list: list[str]) -> int:
"""Return the shortest transformation length, or -1 if impossible.
Args:
begin_word: Starting word.
end_word: Target word.
word_list: Allowed intermediate words.
Returns:
Length of the shortest transformation sequence, or -1.
Examples:
>>> ladder_length('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log'])
5
"""
if len(begin_word) != len(end_word):
return -1
if begin_word == end_word:
return 0
if sum(c1 != c2 for c1, c2 in zip(begin_word, end_word, strict=False)) == 1:
return 1
begin_set: set[str] = set()
end_set: set[str] = set()
begin_set.add(begin_word)
end_set.add(end_word)
result = 2
while begin_set and end_set:
if len(begin_set) > len(end_set):
begin_set, end_set = end_set, begin_set
next_begin_set: set[str] = set()
for word in begin_set:
for ladder_word in _word_range(word):
if ladder_word in end_set:
return result
if ladder_word in word_list:
next_begin_set.add(ladder_word)
word_list.remove(ladder_word)
begin_set = next_begin_set
result += 1
return -1
def _word_range(word: str) -> Iterator[str]:
"""Yield all words that differ from *word* by exactly one letter.
Args:
word: The source word.
Yields:
Words with a single character changed.
"""
for ind in range(len(word)):
temp = word[ind]
for c in [chr(x) for x in range(ord("a"), ord("z") + 1)]:
if c != temp:
yield word[:ind] + c + word[ind + 1 :]