Skip to content

127. Word Ladder#20

Open
tarinaihitori wants to merge 1 commit into
mainfrom
127-word-ladder
Open

127. Word Ladder#20
tarinaihitori wants to merge 1 commit into
mainfrom
127-word-ladder

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

Comment thread 127. Word Ladder.md
def make_transformation_patterns(word):
patterns = []
for i in range(len(word)):
pattern = word[:i] + "*" + word[i+1:]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"*" が来ると動かなくなるのが気になり、Python の場合は、タプルも dict の Key にできるのでそれも一つかなと思います。

Comment thread 127. Word Ladder.md
for i in range(len(word)):
pattern = word[:i] + "*" + word[i+1:]
patterns.append(pattern)
return patterns
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

少し応用ですが yield を使う文法は見ておいてもいいかもしれません。

Comment thread 127. Word Ladder.md
Comment on lines +22 to +27
for char in 'abcdefghijklmnopqrstuvwxyz':
if char == current_word[i]:
continue
next_word = current_word[:i] + char + current_word[i+1:]

if next_word in wordList:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここを別の関数に切り出すと少し読みやすいでしょう。yeild を使うのも一つです。

Comment thread 127. Word Ladder.md
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
queue = deque()
queue.append((beginWord, 1)) # 現在の単語と変換ステップ数
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 は色々な書き方ありますが、私はペアを突っ込むのが個人的には好みです。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

PythonだとTLEになる場合もありますが、素直に隣接リストを作って最短経路を求める解法が素直でいいかなと思います。

Comment thread 127. Word Ladder.md

if next_word in wordList:
queue.append((next_word, step_count + 1))
wordList.remove(next_word)
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を破壊しているのが気になりました。

Comment thread 127. Word Ladder.md
```

wordList を set にして通った。
時間計算量:(N・M) M は単語の長さ
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ノードの数がN個で、各ノードでM文字分ループを回して(L50)、その中でM文字のwordに対してスライスを取っている(L54)なので、N * M^2になるかなと思います。

Comment thread 127. Word Ladder.md
pattern_to_words[pattern].append(word)

words = [beginWord]
seen = set()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

seenにbeginWordを追加しても良いかなと思いまいした。課題の制約やBFSの性質的に入れなくても問題ないのですが。

Comment thread 127. Word Ladder.md
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
def is_adjacent(word1: str, word2: str) -> bool:
differences = sum(1 for a, b in zip(word1, word2) if a != b)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ちなみにこれはHamming Distanceに相当しますね。こちらもEdit Distanceの1つです。

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.

3 participants