Skip to content

Commit

Permalink
Create 0127-word-ladder.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AP-Repositories authored Jan 4, 2023
1 parent 6bfb5ff commit 14646c0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions go/0127-word-ladder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
func ladderLength(beginWord string, endWord string, wordList []string) int {
if !contains(wordList, endWord) {
return 0
}

nei := make(map[string][]string)
wordList = append(wordList, beginWord)
for _, word := range wordList {
for j := 0; j < len(word); j++ {
pattern := word[:j] + "*" + word[j + 1:]
nei[pattern] = append(nei[pattern], word)
}
}

visit := map[string]bool{beginWord: true}
q := []string{beginWord}
res := 1
for len(q) != 0 {
for tmp := len(q); tmp > 0; tmp-- {
word := q[0]
q = q[1:]
if word == endWord {
return res
}
for j := 0; j < len(word); j++ {
pattern := word[:j] + "*" + word[j + 1:]
for _, neiWord := range nei[pattern] {
if !visit[neiWord] {
visit[neiWord] = true
q = append(q, neiWord)
}
}
}
}
res += 1
}
return 0
}

func contains(s []string, word string) bool {
for _, element := range s {
if element == word {
return true
}
}
return false
}

0 comments on commit 14646c0

Please sign in to comment.