-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordLadder.py
More file actions
24 lines (22 loc) · 824 Bytes
/
wordLadder.py
File metadata and controls
24 lines (22 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/word-ladder/
# Author: Miao Zhang
# Date: 2021-01-20
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
from collections import deque
wordset = set(wordList)
q = deque()
q.append((beginWord, 1))
while q:
word, length = q.popleft()
if word == endWord:
return length
for i in range(len(word)):
for c in "abcdefghijklmnopqrstuvwxyz":
newword = word[:i] + c + word[i + 1:]
if newword in wordset and newword != word:
wordset.remove(newword)
q.append((newword, length + 1))
return 0