127. Word Ladder#20
Open
tarinaihitori wants to merge 1 commit into
Open
Conversation
oda
reviewed
Nov 21, 2024
| def make_transformation_patterns(word): | ||
| patterns = [] | ||
| for i in range(len(word)): | ||
| pattern = word[:i] + "*" + word[i+1:] |
There was a problem hiding this comment.
"*" が来ると動かなくなるのが気になり、Python の場合は、タプルも dict の Key にできるのでそれも一つかなと思います。
oda
reviewed
Nov 21, 2024
| for i in range(len(word)): | ||
| pattern = word[:i] + "*" + word[i+1:] | ||
| patterns.append(pattern) | ||
| return patterns |
oda
reviewed
Nov 21, 2024
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: |
oda
reviewed
Nov 21, 2024
| class Solution: | ||
| def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
| queue = deque() | ||
| queue.append((beginWord, 1)) # 現在の単語と変換ステップ数 |
hayashi-ay
reviewed
Nov 22, 2024
hayashi-ay
left a comment
There was a problem hiding this comment.
PythonだとTLEになる場合もありますが、素直に隣接リストを作って最短経路を求める解法が素直でいいかなと思います。
|
|
||
| if next_word in wordList: | ||
| queue.append((next_word, step_count + 1)) | ||
| wordList.remove(next_word) |
| ``` | ||
|
|
||
| wordList を set にして通った。 | ||
| 時間計算量:(N・M) M は単語の長さ |
There was a problem hiding this comment.
ノードの数がN個で、各ノードでM文字分ループを回して(L50)、その中でM文字のwordに対してスライスを取っている(L54)なので、N * M^2になるかなと思います。
| pattern_to_words[pattern].append(word) | ||
|
|
||
| words = [beginWord] | ||
| seen = set() |
There was a problem hiding this comment.
seenにbeginWordを追加しても良いかなと思いまいした。課題の制約やBFSの性質的に入れなくても問題ないのですが。
| 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) |
There was a problem hiding this comment.
ちなみにこれはHamming Distanceに相当しますね。こちらもEdit Distanceの1つです。
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.
https://leetcode.com/problems/word-ladder/description/