44import java .util .LinkedList ;
55import java .util .List ;
66import java .util .Queue ;
7+ import java .util .Set ;
78
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.
3912 */
40-
41- final class WordLadder {
13+ public final class WordLadder {
4214 private WordLadder () {
4315 }
4416
4517 /**
46- * This function finds the ladderLength
18+ * Finds the shortest transformation sequence from beginWord to endWord.
4719 *
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
5424 */
5525 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 );
5727
58- if (!set .contains (endWord )) {
28+ if (!wordSet .contains (endWord )) {
5929 return 0 ;
6030 }
6131
@@ -66,25 +36,25 @@ public static int ladderLength(String beginWord, String endWord, List<String> wo
6636 while (!queue .isEmpty ()) {
6737 int size = queue .size ();
6838 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 ];
7343 for (char c = 'a' ; c <= 'z' ; c ++) {
74- if (wordsChars [j ] == c ) {
44+ if (currentWordChars [j ] == c ) {
7545 continue ;
7646 }
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 )) {
8051 return level + 1 ;
8152 }
82- if (set .contains (transformedWord )) {
83- set .remove (transformedWord );
84- queue .offer (transformedWord );
53+ if (wordSet .remove (newWord )) {
54+ queue .offer (newWord );
8555 }
8656 }
87- wordsChars [j ] = originalChars ;
57+ currentWordChars [j ] = originalChar ;
8858 }
8959 }
9060 level ++;
0 commit comments