Skip to content

Commit 7746a41

Browse files
committed
little optimize for word ladder
1 parent 5a68918 commit 7746a41

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

go/word-ladder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func bfs(beginWord, endWord string, pendingWords []string, level int) int {
4646
if pendingWords[i] == endWord {
4747
return level + 2
4848
}
49+
}
50+
51+
for i := 0; i < len(pendingWords); i++ {
4952
llVisited[pendingWords[i]] = true
5053
entries, ok := llWordRelation.GetRelate(pendingWords[i])
5154
if !ok {
@@ -55,6 +58,9 @@ func bfs(beginWord, endWord string, pendingWords []string, level int) int {
5558
if llVisited[word] {
5659
continue
5760
}
61+
if word == endWord {
62+
return level + 3
63+
}
5864
newPendingWords = append(newPendingWords, word)
5965
}
6066
}

0 commit comments

Comments
 (0)