-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathword_ladder.go
69 lines (60 loc) · 1.33 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package graph
import "container/list"
type vertex struct {
word string
edges []*vertex
distance int
}
var graph = make(map[string]*vertex)
// WordLadder solves the problem in O(n) time and O(n) space.
func WordLadder(start, end string, dic []string) int {
graph = make(map[string]*vertex)
graph[start] = &vertex{word: start, edges: nil, distance: 0}
for _, w := range dic {
graph[w] = &vertex{word: w}
}
for w1 := range graph {
for _, w2 := range dic {
if isDifferentByOneLetter(w1, w2) {
graph[w1].edges = append(graph[w1].edges, graph[w2])
}
}
}
return bfsMinTransformation(start, end)
}
func bfsMinTransformation(beginWord string, endWord string) int {
min := 0
distance := 0
source := graph[beginWord]
seen := make(map[*vertex]struct{})
queue := list.New()
queue.PushBack(source)
for queue.Len() != 0 {
tmp := queue.Remove(queue.Front()).(*vertex)
distance++
for _, v := range tmp.edges {
if _, ok := seen[v]; ok {
continue
}
v.distance = distance
seen[v] = struct{}{}
if v.word == endWord && (v.distance < min || min == 0) {
min = v.distance
}
queue.PushBack(v)
}
}
return min
}
func isDifferentByOneLetter(a, b string) bool {
oneDiff := false
for i := range a {
if a[i] != b[i] {
if oneDiff {
return false
}
oneDiff = true
}
}
return oneDiff
}