127. Word Ladder - #60
Open
skypenguins wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
Jul 30, 2026
chryschron
reviewed
Aug 1, 2026
| return distance | ||
|
|
||
| for i in range(len(word)): | ||
| for c in "abcdefghijklmnopqrstuvwxyz": |
There was a problem hiding this comment.
好みの問題になりますが、現在の文字列から文字列を生成し、それがwordListに入っているか確かめる部分は別に関数を作って分けてもいいと思います。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。確かに関数化しても良いですね。
chryschron
reviewed
Aug 1, 2026
| #### 方針 | ||
| - 探索グラフの考え方はOK | ||
| - BFSを使う | ||
| - キューには `(単語, その単語までの距離)` を持たせ、pop時にそのまま返せば最短性が保証される |
There was a problem hiding this comment.
BFSは同じ深さのノードを順番に探索するので、while candidate_words:の次行でcandidate_wordsの大きさ(=同じ深さのノード数)を取得すればqueueで距離を保持する必要がなくなります。
今回はwordListが十分小さいのでそこまでしなくても良いかもしれません。
Owner
Author
There was a problem hiding this comment.
距離情報の保持が不要になるのは確かにおっしゃるとおりですね。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
127. Word Ladder