Created
November 22, 2021 16:47
-
-
Save kevchentw/d89c92982d3e1f9d0e6288d441cc4947 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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