Skip to content

127. Word Ladder - #60

Open
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-127
Open

127. Word Ladder#60
skypenguins wants to merge 1 commit into
mainfrom
leetcode/arai60/problem-127

Conversation

@skypenguins

Copy link
Copy Markdown
Owner

127. Word Ladder

@skypenguins skypenguins self-assigned this Jul 29, 2026
Comment thread memo.md
return distance

for i in range(len(word)):
for c in "abcdefghijklmnopqrstuvwxyz":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string.ascii_lowercase を使うことをお勧めいたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
そのような定数が用意されていることを初めて知りました。(公式ドキュメント

Comment thread memo.md
return distance

for i in range(len(word)):
for c in "abcdefghijklmnopqrstuvwxyz":

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みの問題になりますが、現在の文字列から文字列を生成し、それがwordListに入っているか確かめる部分は別に関数を作って分けてもいいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。確かに関数化しても良いですね。

Comment thread memo.md
#### 方針
- 探索グラフの考え方はOK
- BFSを使う
- キューには `(単語, その単語までの距離)` を持たせ、pop時にそのまま返せば最短性が保証される

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BFSは同じ深さのノードを順番に探索するので、while candidate_words:の次行でcandidate_wordsの大きさ(=同じ深さのノード数)を取得すればqueueで距離を保持する必要がなくなります。
今回はwordListが十分小さいのでそこまでしなくても良いかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

距離情報の保持が不要になるのは確かにおっしゃるとおりですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants