4
4
import java .util .LinkedList ;
5
5
import java .util .List ;
6
6
import java .util .Queue ;
7
+ import java .util .Set ;
7
8
8
- /*
9
- **Problem Statement:**
10
- A transformation sequence from word beginWord to word endWord using a dictionary wordList is a
11
- sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
12
-
13
- Every adjacent pair of words differs by a single letter.
14
- Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
15
- sk == endWord
16
- Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in
17
- the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
18
-
19
- **Example 1:**
20
- Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
21
- Output: 5
22
- Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog",
23
- which is 5 words long.
24
-
25
- **Example 2:**
26
- Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
27
- Output: 0
28
- Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation
29
- sequence.
30
-
31
- **Constraints:**
32
- 1 <= beginWord.length <= 10
33
- endWord.length == beginWord.length
34
- 1 <= wordList.length <= 5000
35
- wordList[i].length == beginWord.length
36
- beginWord, endWord, and wordList[i] consist of lowercase English letters.
37
- beginWord != endWord
38
- All the words in wordList are unique.
9
+ /**
10
+ * Class to find the shortest transformation sequence from a beginWord to an endWord using a dictionary of words.
11
+ * A transformation sequence is a sequence of words where each adjacent pair differs by exactly one letter.
39
12
*/
40
-
41
- final class WordLadder {
13
+ public final class WordLadder {
42
14
private WordLadder () {
43
15
}
44
16
45
17
/**
46
- * This function finds the ladderLength
18
+ * Finds the shortest transformation sequence from beginWord to endWord.
47
19
*
48
- * @param beginWord: Starting word of the ladder
49
- * @param endWord: Ending word of the ladder
50
- * @param wordList: This list contains the words which needs to be included
51
- * in ladder.
52
- * @return ladderLength: This function will return the ladderLength(level)
53
- * if the endword is there. Otherwise, will return the length as 0.
20
+ * @param beginWord the starting word of the transformation sequence
21
+ * @param endWord the target word of the transformation sequence
22
+ * @param wordList a list of words that can be used in the transformation sequence
23
+ * @return the number of words in the shortest transformation sequence, or 0 if no such sequence exists
54
24
*/
55
25
public static int ladderLength (String beginWord , String endWord , List <String > wordList ) {
56
- HashSet <String > set = new HashSet <>(wordList );
26
+ Set <String > wordSet = new HashSet <>(wordList );
57
27
58
- if (!set .contains (endWord )) {
28
+ if (!wordSet .contains (endWord )) {
59
29
return 0 ;
60
30
}
61
31
@@ -66,25 +36,26 @@ public static int ladderLength(String beginWord, String endWord, List<String> wo
66
36
while (!queue .isEmpty ()) {
67
37
int size = queue .size ();
68
38
for (int i = 0 ; i < size ; i ++) {
69
- String curr = queue .poll ();
70
- char [] wordsChars = curr .toCharArray ();
71
- for (int j = 0 ; j < wordsChars .length ; j ++) {
72
- char originalChars = wordsChars [j ];
39
+ String currentWord = queue .poll ();
40
+ char [] currentWordChars = currentWord .toCharArray ();
41
+ for (int j = 0 ; j < currentWordChars .length ; j ++) {
42
+ char originalChar = currentWordChars [j ];
73
43
for (char c = 'a' ; c <= 'z' ; c ++) {
74
- if (wordsChars [j ] == c ) {
44
+ if (currentWordChars [j ] == c ) {
75
45
continue ;
76
46
}
77
- wordsChars [j ] = c ;
78
- String transformedWord = String .valueOf (wordsChars );
79
- if (transformedWord .equals (endWord )) {
47
+ currentWordChars [j ] = c ;
48
+ String newWord = new String (currentWordChars );
49
+
50
+ if (newWord .equals (endWord )) {
80
51
return level + 1 ;
81
52
}
82
- if (set .contains (transformedWord )) {
83
- set .remove (transformedWord );
84
- queue .offer (transformedWord );
53
+ if (wordSet .contains (newWord )) {
54
+ wordSet .remove (newWord );
55
+ queue .offer (newWord );
85
56
}
86
57
}
87
- wordsChars [j ] = originalChars ;
58
+ currentWordChars [j ] = originalChar ;
88
59
}
89
60
}
90
61
level ++;
0 commit comments