Skip to content

Commit 82eb9c2

Browse files
author
alxkm
committed
refactor: WordLadder
1 parent c57e02d commit 82eb9c2

File tree

2 files changed

+35
-54
lines changed

2 files changed

+35
-54
lines changed

src/main/java/com/thealgorithms/strings/WordLadder.java

+25-54
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,28 @@
44
import java.util.LinkedList;
55
import java.util.List;
66
import 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,26 @@ 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.contains(newWord)) {
54+
wordSet.remove(newWord);
55+
queue.offer(newWord);
8556
}
8657
}
87-
wordsChars[j] = originalChars;
58+
currentWordChars[j] = originalChar;
8859
}
8960
}
9061
level++;

src/test/java/com/thealgorithms/strings/WordLadderTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Arrays;
66
import java.util.List;
77
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.CsvSource;
810

911
public class WordLadderTest {
1012

@@ -38,4 +40,12 @@ public void testWordLadder2() {
3840
List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
3941
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
4042
}
43+
44+
@ParameterizedTest
45+
@CsvSource({"'a', 'c', 'b,c', 2", "'a', 'c', 'a', 0", "'a', 'a', 'a', 0", "'ab', 'cd', 'ad,bd,cd', 3", "'a', 'd', 'b,c,d', 2", "'a', 'd', 'b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,d', 2"})
46+
void testLadderLength(String beginWord, String endWord, String wordListStr, int expectedLength) {
47+
List<String> wordList = List.of(wordListStr.split(","));
48+
int result = WordLadder.ladderLength(beginWord, endWord, wordList);
49+
assertEquals(expectedLength, result);
50+
}
4151
}

0 commit comments

Comments
 (0)