-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0127-word-ladder.go
47 lines (44 loc) · 1.23 KB
/
0127-word-ladder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
}