You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/1-1000/127-word-ladder.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ The **word transformation sequence** problem can be abstracted into a **graph th
51
51
*`Breadth-First Search` emphasizes first-in-first-out, so a **queue** is needed.
52
52
53
53
## Approach
54
-
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round.
54
+
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration.
55
55
1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result.
-`startGene`, `endGene`, and `bank[i]` consist of only the characters `['A', 'C', 'G', 'T']`.
31
31
32
-
## Intuition
32
+
## Intuition 1
33
33
We can think of this problem as a shortest path problem on a graph: there are `4^8` vertices (strings `'AAAAAAAA'` to `'TTTTTTTT'`), and there is an edge between two vertices if they differ in one character, and if *both* vertices are in `bank`.
34
34
35
35
So this issue can be solved by **Breadth-First Search** on a undirected graph.
36
36
37
-
### Solution 1: Breadth-First Search
38
37

39
38
40
39
* As shown in the figure above, **Breadth-First Search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
41
40
getting `minimum number` of something of a graph, `Breadth-First Search` would probably help.
42
41
43
42
*`Breadth-First Search` emphasizes first-in-first-out, so a **queue** is needed.
44
43
45
-
####Approach
46
-
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round.
47
-
1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result.
44
+
### Approach 1: Breadth-First Search Algorithm
45
+
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration.
46
+
1. So through `Breadth-First Search`, when a word matches `endGene`, the game is over, and we can return the number of **circle** as a result.
48
47
49
-
####Complexity
48
+
### Complexity
50
49
> **N** is the length of `bank`.
51
50
52
51
* Time: `O((8 * 4) * N)`.
53
52
* Space: `O(N)`.
54
53
55
-
### Solution 2: A* (A-Star) Search Algorithm
54
+
##Approach 2: A* (A-Star) Search Algorithm
56
55
57
-
**A-Star Search Algorithm** can be used to improve the performance of **Breadth-First Search Algorithm**.
56
+
**A-Star Search Algorithm** can be used to greatly improve the performance of **Breadth-First Search Algorithm**.
58
57
59
-
Please view **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
58
+
Please view detailed **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
60
59
61
-
Bellow is code using _A-Star Search Algorithm_ in Python.
60
+
Bellow has code using _A-Star Search Algorithm_ in Python.
-`startGene`, `endGene`, and `bank[i]` consist of only the characters `['A', 'C', 'G', 'T']`.
31
31
32
-
## Intuition
32
+
## Intuition 1
33
33
We can think of this problem as a shortest path problem on a graph: there are `4^8` vertices (strings `'AAAAAAAA'` to `'TTTTTTTT'`), and there is an edge between two vertices if they differ in one character, and if *both* vertices are in `bank`.
34
34
35
35
So this issue can be solved by **Breadth-First Search** on a undirected graph.
36
36
37
-
### Solution 1: Breadth-First Search
38
37

39
38
40
39
* As shown in the figure above, **Breadth-First Search** can be thought of as visiting vertices in rounds and rounds. Actually, whenever you see a question is about
41
40
getting `minimum number` of something of a graph, `Breadth-First Search` would probably help.
42
41
43
42
*`Breadth-First Search` emphasizes first-in-first-out, so a **queue** is needed.
44
43
45
-
####Approach
46
-
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration, but we can simplify it by using just 1 round.
47
-
1. So through `Breadth-First Search`, when a word matches `endWord`, the game is over, and we can return the number of **circle** as a result.
44
+
### Approach 1: Breadth-First Search Algorithm
45
+
1.`Breadth-First Search` a graph means traversing **from near to far**, from `circle 1` to `circle N`. Each `circle` is a round of iteration.
46
+
1. So through `Breadth-First Search`, when a word matches `endGene`, the game is over, and we can return the number of **circle** as a result.
48
47
49
-
####Complexity
48
+
### Complexity
50
49
> **N** is the length of `bank`.
51
50
52
51
* Time: `O((8 * 4) * N)`.
53
52
* Space: `O(N)`.
54
53
55
-
### Solution 2: A* (A-Star) Algorithm
54
+
##Approach 2: A* (A-Star) Search Algorithm
56
55
57
-
**A-Star Algorithm** can be used to improve the performance of **Breadth-First Search Algorithm**.
56
+
**A-Star Search Algorithm** can be used to greatly improve the performance of **Breadth-First Search Algorithm**.
58
57
59
-
Please view **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
58
+
Please view detailed **A-Star Algorithm** at [752. Open the Lock](./752-open-the-lock.md).
60
59
61
-
Bellow is code using _A-Star Algorithm_ in Python.
60
+
Bellow has code using _A-Star Search Algorithm_ in Python.
0 commit comments