Skip to content

127. Word Ladder#19

Open
tNita wants to merge 1 commit into
mainfrom
add/127_word_ladder
Open

127. Word Ladder#19
tNita wants to merge 1 commit into
mainfrom
add/127_word_ladder

Conversation

@tNita
Copy link
Copy Markdown
Owner

@tNita tNita commented May 20, 2026

この問題: Word Ladder
次の問題: Maximum Depth of Binary Tree

Comment thread 127_word_ladder/memo.md
if letter == current_word[change_at]:
continue
new_word_candidates = current_word[:change_at] + letter + current_word[change_at+1:]
if new_word_candidates in wordList and new_word_candidates not in previous_sequence:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

previous_sequence は1つの経路しか記録していないので、他の分岐が既に探索してきた経路と知らずに計算してしまいます。

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.

@quinn-sasha

確かに、経路が部分的に重複している箇所がたくさんありそうですね。

ご指摘ありがとうございます。

Comment thread 127_word_ladder/memo.md
- TTLでアクセプトしなかった
- 24 / 52 testcases passed
- どこがボトルネックなのだろう?
- 毎回deeepcopyしているところ?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

previous_sequence のdeepcopy もそうですね。
あとは DFS で全ての経路を探索していることと、リストに対してin検索していることですかね。

Copy link
Copy Markdown

@h-masder h-masder May 21, 2026

Choose a reason for hiding this comment

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

混乱してしまい、わからなくなってしまったためStep2に行く

深さ優先探索で全ての経路を探索していることがボトルネックですかね。

L: word の長さ
N: wordList の長さ

54行目の処理: O(L)
44行目の in 検索(list に対する探索: O(N)

この処理を、深さ優先探索によって候補となる word の経路数(最大で O((25L)^N) 程度)だけ繰り返しているため、全体として指数時間に近い計算量になっているようです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

O((25L)^N) の部分ですが、例えば beginWord = "aa" の場合を考えると、
最初の候補として、
"ba", "ca", ..., "za"
のような word が生成されます。
次に "ba" を探索する際には、
"bb", "bc", ..., "bz"
のような候補を調べていきます。
今度は "ca" に対して同様の探索を行う……という流れで、順番に探索していく形になります。

aa
├─ ba
│ ├─ bb
│ ├─ bc
│ ├─ ...
│ └─ bz
├─ ca
│ ├─ cb
│ ├─ cc
│ ├─ ...
│ └─ cz
├─ da
│ └─ ...
└─ ...

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.

@quinn-sasha

あとは DFS で全ての経路を探索していることと、リストに対してin検索していることですかね。

確かにそうですね。コメントありがとうございます。

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.

@h-masder

この処理を、深さ優先探索によって候補となる word の経路数(最大で O((25L)^N) 程度)だけ繰り返しているため、全体として指数時間に近い計算量になっているようです。

コメントありがとうございます。すみません、↑についてまだ理解できていなく質問させてください🙇

25Lと、そのN乗はそれぞれ何を表していますでしょうか?

Comment thread 127_word_ladder/memo.md

"""
class Solution:
LOWER_ENGLISH_LETTERS = "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 が使えそうです。

https://docs.python.org/ja/3.14/library/string.html#string.ascii_lowercase

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants