Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7149b1

Browse files
aQuaaQua
aQua
authored and
aQua
committed
126 Time Limited Exceeded
1 parent dfe465d commit c7149b1

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [126. Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)
2+
3+
## 题目
4+
Given two words (beginWord and endWord), and a dictionary's word list, find **all shortest** transformation sequence(s) from beginWord to endWord, such that:
5+
1. Only one letter can be changed at a time
6+
1. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
7+
8+
For example,
9+
```
10+
Given:
11+
beginWord = "hit"
12+
endWord = "cog"
13+
wordList = ["hot","dot","dog","lot","log","cog"]
14+
15+
Return
16+
[
17+
["hit","hot","dot","dog","cog"],
18+
["hit","hot","lot","log","cog"]
19+
]
20+
```
21+
22+
Note:
23+
1. Return an empty list if there is no such transformation sequence.
24+
1. All words have the same length.
25+
1. All words contain only lowercase alphabetic characters.
26+
1. You may assume no duplicates in the word list.
27+
1. You may assume beginWord and endWord are non-empty and are not the same.
28+
29+
UPDATE (2017/1/20):
30+
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
31+
32+
## 解题思路
33+
34+
见程序注释
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package Problem0126
2+
3+
func findLadders(beginWord string, endWord string, words []string) [][]string {
4+
res := [][]string{}
5+
6+
isUsed := map[string]bool{}
7+
8+
transMap := map[string][]string{}
9+
for i := 0; i < len(words); i++ {
10+
if words[i] == beginWord {
11+
// Note that beginWord is not a transformed word.
12+
continue
13+
}
14+
15+
if isTransable(words[i], beginWord) {
16+
transMap[beginWord] = append(transMap[beginWord], words[i])
17+
}
18+
19+
for j := i + 1; j < len(words); j++ {
20+
if words[j] == beginWord {
21+
// Note that beginWord is not a transformed word.
22+
continue
23+
}
24+
25+
a, b := words[i], words[j]
26+
if isTransable(a, b) {
27+
transMap[a] = append(transMap[a], b)
28+
transMap[b] = append(transMap[b], a)
29+
}
30+
}
31+
}
32+
33+
minLen := 1<<63 - 1
34+
35+
var dfs func(string, []string, int)
36+
dfs = func(word string, path []string, idx int) {
37+
tempLen := idx + 1
38+
if minLen < tempLen {
39+
return
40+
}
41+
42+
for _, w := range transMap[word] {
43+
if isUsed[w] {
44+
continue
45+
}
46+
47+
path[idx] = w
48+
if w == endWord {
49+
temp := make([]string, tempLen)
50+
copy(temp, path)
51+
52+
if minLen > tempLen {
53+
minLen = tempLen
54+
res = [][]string{temp}
55+
} else {
56+
// minLen == tempLen
57+
res = append(res, temp)
58+
}
59+
return
60+
}
61+
isUsed[w] = true
62+
dfs(w, path, idx+1)
63+
isUsed[w] = false
64+
}
65+
}
66+
67+
path := make([]string, len(words)*2)
68+
path[0] = beginWord
69+
dfs(beginWord, path, 1)
70+
71+
return res
72+
}
73+
74+
func isTransable(a, b string) bool {
75+
diff := false
76+
for i := range a {
77+
if a[i] != b[i] {
78+
if !diff {
79+
diff = true
80+
} else {
81+
return false
82+
}
83+
}
84+
}
85+
86+
return true
87+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package Problem0126
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_Problem0126(t *testing.T) {
11+
ast := assert.New(t)
12+
13+
// tcs is testcase slice
14+
tcs := []struct {
15+
beginWord string
16+
endWord string
17+
wordList []string
18+
ans [][]string
19+
}{
20+
21+
{
22+
"qa",
23+
"sq",
24+
[]string{"si", "go", "se", "cm", "so", "ph", "mt", "db", "mb", "sb", "kr", "ln", "tm", "le", "av", "sm", "ar", "ci", "ca", "br", "ti", "ba", "to", "ra", "fa", "yo", "ow", "sn", "ya", "cr", "po", "fe", "ho", "ma", "re", "or", "rn", "au", "ur", "rh", "sr", "tc", "lt", "lo", "as", "fr", "nb", "yb", "if", "pb", "ge", "th", "pm", "rb", "sh", "co", "ga", "li", "ha", "hz", "no", "bi", "di", "hi", "qa", "pi", "os", "uh", "wm", "an", "me", "mo", "na", "la", "st", "er", "sc", "ne", "mn", "mi", "am", "ex", "pt", "io", "be", "fm", "ta", "tb", "ni", "mr", "pa", "he", "lr", "sq", "ye"},
25+
[][]string{
26+
[]string{"qa", "ba", "be", "se", "sq"},
27+
[]string{"qa", "ba", "bi", "si", "sq"},
28+
[]string{"qa", "ba", "br", "sr", "sq"},
29+
[]string{"qa", "ca", "cm", "sm", "sq"},
30+
[]string{"qa", "ca", "co", "so", "sq"},
31+
[]string{"qa", "la", "ln", "sn", "sq"},
32+
[]string{"qa", "la", "lt", "st", "sq"},
33+
[]string{"qa", "ma", "mb", "sb", "sq"},
34+
[]string{"qa", "pa", "ph", "sh", "sq"},
35+
[]string{"qa", "ta", "tc", "sc", "sq"},
36+
[]string{"qa", "fa", "fe", "se", "sq"},
37+
[]string{"qa", "ga", "ge", "se", "sq"},
38+
[]string{"qa", "ha", "he", "se", "sq"},
39+
[]string{"qa", "la", "le", "se", "sq"},
40+
[]string{"qa", "ma", "me", "se", "sq"},
41+
[]string{"qa", "na", "ne", "se", "sq"},
42+
[]string{"qa", "ra", "re", "se", "sq"},
43+
[]string{"qa", "ya", "ye", "se", "sq"},
44+
[]string{"qa", "ca", "ci", "si", "sq"},
45+
[]string{"qa", "ha", "hi", "si", "sq"},
46+
[]string{"qa", "la", "li", "si", "sq"},
47+
[]string{"qa", "ma", "mi", "si", "sq"},
48+
[]string{"qa", "na", "ni", "si", "sq"},
49+
[]string{"qa", "pa", "pi", "si", "sq"},
50+
[]string{"qa", "ta", "ti", "si", "sq"},
51+
[]string{"qa", "ca", "cr", "sr", "sq"},
52+
[]string{"qa", "fa", "fr", "sr", "sq"},
53+
[]string{"qa", "la", "lr", "sr", "sq"},
54+
[]string{"qa", "ma", "mr", "sr", "sq"},
55+
[]string{"qa", "fa", "fm", "sm", "sq"},
56+
[]string{"qa", "pa", "pm", "sm", "sq"},
57+
[]string{"qa", "ta", "tm", "sm", "sq"},
58+
[]string{"qa", "ga", "go", "so", "sq"},
59+
[]string{"qa", "ha", "ho", "so", "sq"},
60+
[]string{"qa", "la", "lo", "so", "sq"},
61+
[]string{"qa", "ma", "mo", "so", "sq"},
62+
[]string{"qa", "na", "no", "so", "sq"},
63+
[]string{"qa", "pa", "po", "so", "sq"},
64+
[]string{"qa", "ta", "to", "so", "sq"},
65+
[]string{"qa", "ya", "yo", "so", "sq"},
66+
[]string{"qa", "ma", "mn", "sn", "sq"},
67+
[]string{"qa", "ra", "rn", "sn", "sq"},
68+
[]string{"qa", "ma", "mt", "st", "sq"},
69+
[]string{"qa", "pa", "pt", "st", "sq"},
70+
[]string{"qa", "na", "nb", "sb", "sq"},
71+
[]string{"qa", "pa", "pb", "sb", "sq"},
72+
[]string{"qa", "ra", "rb", "sb", "sq"},
73+
[]string{"qa", "ta", "tb", "sb", "sq"},
74+
[]string{"qa", "ya", "yb", "sb", "sq"},
75+
[]string{"qa", "ra", "rh", "sh", "sq"},
76+
[]string{"qa", "ta", "th", "sh", "sq"},
77+
},
78+
},
79+
80+
{
81+
"hit",
82+
"cog",
83+
[]string{"hot", "dot", "dog", "lot", "log"},
84+
[][]string{},
85+
},
86+
87+
{
88+
"hot",
89+
"dog",
90+
[]string{"hot", "dog", "dot"},
91+
[][]string{
92+
[]string{"hot", "dot", "dog"},
93+
},
94+
},
95+
96+
{
97+
"hit",
98+
"cog",
99+
[]string{"hot", "dot", "dog", "lot", "log", "cog"},
100+
[][]string{
101+
[]string{"hit", "hot", "dot", "dog", "cog"},
102+
[]string{"hit", "hot", "lot", "log", "cog"},
103+
},
104+
},
105+
106+
// 可以多个 testcase
107+
}
108+
109+
for _, tc := range tcs {
110+
fmt.Printf("~~%v~~\n", tc)
111+
112+
ast.Equal(tc.ans, findLadders(tc.beginWord, tc.endWord, tc.wordList), "输入:%v", tc)
113+
}
114+
}

0 commit comments

Comments
 (0)