Skip to content

Instantly share code, notes, and snippets.

@kevchentw
Created November 22, 2021 16:47
Show Gist options
  • Select an option

  • Save kevchentw/d89c92982d3e1f9d0e6288d441cc4947 to your computer and use it in GitHub Desktop.

Select an option

Save kevchentw/d89c92982d3e1f9d0e6288d441cc4947 to your computer and use it in GitHub Desktop.
from collections import deque
def l():
bank = ["hot","dot","dog","lot","log","cog"]
start = "hit"
end = "cog"
q = deque([[start, 1]])
while q:
word, length = q.popleft()
if word == end:
print(word, length)
return length
for i in range(len(start)):
for c in "abcdefghijklmnopqrstuvwxyz":
next_word = word[:i] + c + word[i+1:]
if next_word in bank:
bank.remove(next_word)
q.append([next_word, length+1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment