1
+ package WordLadder ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .HashSet ;
5
+ import java .util .List ;
6
+ import java .util .Set ;
7
+
8
+ /**
9
+ * User: Danyang
10
+ * Date: 1/30/2015
11
+ * Time: 22:30
12
+ *
13
+ * Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to
14
+ * end, such that:
15
+
16
+ Only one letter can be changed at a time
17
+ Each intermediate word must exist in the dictionary
18
+ For example,
19
+
20
+ Given:
21
+ start = "hit"
22
+ end = "cog"
23
+ dict = ["hot","dot","dog","lot","log"]
24
+ As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
25
+ return its length 5.
26
+
27
+ Note:
28
+ Return 0 if there is no such transformation sequence.
29
+ All words have the same length.
30
+ All words contain only lowercase alphabetic characters.
31
+ */
32
+ public class Solution {
33
+ /**
34
+ * Algorithm bfs
35
+ *
36
+ * Notice:
37
+ * 1. TLE due to the timing of remove the string from dict;
38
+ * @param start
39
+ * @param end
40
+ * @param dict
41
+ * @return
42
+ */
43
+ public int ladderLength_TLE (String start , String end , Set <String > dict ) {
44
+ List <String > q = new ArrayList <>();
45
+ q .add (start );
46
+ int level = 1 ;
47
+ while (!q .isEmpty ()) {
48
+ int l = q .size ();
49
+ level ++;
50
+
51
+ for (int i =0 ; i <l ; i ++) {
52
+ String cur = q .get (i );
53
+ dict .remove (cur );
54
+
55
+ for (int j =0 ; j <cur .length (); j ++) {
56
+ char [] c = cur .toCharArray ();
57
+ for (char char1 ='a' ; char1 <='z' ; char1 ++) {
58
+ c [j ] = char1 ;
59
+ // String next = c.toString(); // bug
60
+ String next = new String (c );
61
+ if (next .equals (end ))
62
+ return level ;
63
+ if (dict .contains (next ))
64
+ q .add (next );
65
+ }
66
+ }
67
+ }
68
+ q = q .subList (l , q .size ());
69
+ }
70
+ return 0 ;
71
+ }
72
+
73
+ public int ladderLength (String start , String end , Set <String > dict ) {
74
+ List <String > q = new ArrayList <>();
75
+ q .add (start );
76
+ dict .remove (start );
77
+ int level = 1 ;
78
+ while (!q .isEmpty ()) {
79
+ int l = q .size ();
80
+ level ++;
81
+
82
+ for (int i =0 ; i <l ; i ++) {
83
+ String cur = q .get (i );
84
+
85
+ for (int j =0 ; j <cur .length (); j ++) {
86
+ char [] c = cur .toCharArray ();
87
+ for (char char1 ='a' ; char1 <='z' ; char1 ++) {
88
+ c [j ] = char1 ;
89
+ // String next = c.toString(); // bug
90
+ String next = new String (c );
91
+ if (next .equals (end ))
92
+ return level ;
93
+ if (dict .contains (next )) {
94
+ q .add (next );
95
+ dict .remove (next );
96
+ }
97
+
98
+ }
99
+ }
100
+ }
101
+ q = q .subList (l , q .size ());
102
+ }
103
+ return 0 ;
104
+ }
105
+
106
+ public static void main (String [] args ) {
107
+ Set <String > dict = new HashSet <>();
108
+ dict .add ("a" );
109
+ dict .add ("b" );
110
+ dict .add ("c" );
111
+ int ret = new Solution ().ladderLength ("a" , "c" , dict );
112
+ System .out .println (ret );
113
+ }
114
+ }
0 commit comments