Skip to content

Commit bb3ce1d

Browse files
committed
feat: word-ladder-challenge challenge
1 parent 2285d71 commit bb3ce1d

File tree

9 files changed

+36
-0
lines changed

9 files changed

+36
-0
lines changed

2-NORMAL/Python/11-merge-overlapping-intervals/proposed-solution/intervals.py renamed to 2-NORMAL/Python/6-merge-overlapping-intervals/proposed-solution/intervals.py

File renamed without changes.

2-NORMAL/Python/11-merge-overlapping-intervals/proposed-solution/main.py renamed to 2-NORMAL/Python/6-merge-overlapping-intervals/proposed-solution/main.py

File renamed without changes.

2-NORMAL/Python/12-validate-parentheses/proposed-solution/main.py renamed to 2-NORMAL/Python/7-validate-parentheses/proposed-solution/main.py

File renamed without changes.

2-NORMAL/Python/12-validate-parentheses/proposed-solution/parentheses.py renamed to 2-NORMAL/Python/7-validate-parentheses/proposed-solution/parentheses.py

File renamed without changes.

2-NORMAL/Python/13-minimum-path-sum/proposed-solution/__pycache__/min_path_sum.cpython-312.pyc renamed to 2-NORMAL/Python/8-minimum-path-sum/proposed-solution/__pycache__/min_path_sum.cpython-312.pyc

File renamed without changes.

2-NORMAL/Python/13-minimum-path-sum/proposed-solution/main.py renamed to 2-NORMAL/Python/8-minimum-path-sum/proposed-solution/main.py

File renamed without changes.

2-NORMAL/Python/13-minimum-path-sum/proposed-solution/min_path_sum.py renamed to 2-NORMAL/Python/8-minimum-path-sum/proposed-solution/min_path_sum.py

File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Challenge: Given two words (start and end) and a dictionary, implement an algorithm to find the shortest transformation sequence where each word differs by exactly one character from the previous one. This challenge requires applying search techniques like BFS.
2+
3+
from word_ladder import word_ladder
4+
5+
def main():
6+
begin_word = "hit"
7+
end_word = "cog"
8+
word_list = ["hot", "dot", "dog", "lot", "log", "cog"]
9+
path = word_ladder(begin_word, end_word, word_list)
10+
print("Shortest transformation sequence:", path)
11+
12+
if __name__ == "__main__":
13+
main()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import deque
2+
3+
def word_ladder(begin_word, end_word, word_list):
4+
word_set = set(word_list)
5+
if end_word not in word_set:
6+
return []
7+
8+
queue = deque([(begin_word, [begin_word])])
9+
visited = set([begin_word])
10+
11+
while queue:
12+
current_word, path = queue.popleft()
13+
if current_word == end_word:
14+
return path
15+
16+
for i in range(len(current_word)):
17+
for c in 'abcdefghijklmnopqrstuvwxyz':
18+
next_word = current_word[:i] + c + current_word[i+1:]
19+
if next_word in word_set and next_word not in visited:
20+
visited.add(next_word)
21+
queue.append((next_word, path + [next_word]))
22+
23+
return []

0 commit comments

Comments
 (0)