Skip to content

Commit a0d03e8

Browse files
Update WordLadderTest.java & WordLadder.java (TheAlgorithms#3674)
1 parent 2e25f89 commit a0d03e8

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,8 @@
3434
beginWord != endWord
3535
All the words in wordList are unique.
3636
*/
37-
class WordLadder {
38-
39-
/**
40-
* Driver Code
41-
*/
42-
public static void main(String[] args) {
43-
String beginWord = "hit";
44-
String endWord = "cog";
45-
String words[] = { "hot", "dot", "dog", "lot", "log", "cog" };
46-
List<String> wordList = Arrays.asList(words);
4737

48-
System.out.println(
49-
"Ladder Length: " + ladderLength(beginWord, endWord, wordList)
50-
);
51-
}
38+
class WordLadder {
5239

5340
/**
5441
* This function finds the ladderLength
@@ -60,11 +47,7 @@ public static void main(String[] args) {
6047
* @return ladderLength: This function will return the ladderLength(level)
6148
* if the endword is there. Otherwise, will return the length as 0.
6249
*/
63-
public static int ladderLength(
64-
String beginWord,
65-
String endWord,
66-
List<String> wordList
67-
) {
50+
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
6851
HashSet<String> set = new HashSet();
6952
for (String word : wordList) {
7053
set.add(word);
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.*;
4-
54
import org.junit.jupiter.api.Test;
65
import java.util.*;
76

87
public class WordLadderTest {
98

109
@Test
1110
public void testWordLadder() {
12-
String words1[] = { "hot", "dot", "dog", "lot", "log", "cog" };
13-
assertEquals(5, WordLadder.ladderLength("hit", "cog", Arrays.asList(words1)));
14-
String words2[] = { "hot", "dot", "dog", "lot", "log" };
15-
assertEquals(0, WordLadder.ladderLength("hit", "cog", Arrays.asList(words2)));
11+
12+
/**
13+
* Test 1:
14+
* Input: beginWord = "hit", endWord = "cog", wordList =
15+
* ["hot","dot","dog","lot","log","cog"]
16+
* Output: 5
17+
* Explanation: One shortest transformation sequence is
18+
* "hit" -> "hot" -> "dot" -> "dog" -> cog"
19+
* which is 5 words long.
20+
*/
21+
22+
List<String> wordList1 = Arrays.asList("hot", "dot", "dog", "lot", "log", "cog");
23+
assertEquals(WordLadder.ladderLength("hit", "cog", wordList1), 5);
24+
25+
/**
26+
* Test 2:
27+
* Input: beginWord = "hit", endWord = "cog", wordList =
28+
* ["hot","dot","dog","lot","log"]
29+
* Output: 0
30+
* Explanation: The endWord "cog" is not in wordList,
31+
* therefore there is no valid transformation sequence.
32+
*/
33+
34+
List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
35+
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
36+
1637
}
1738
}

0 commit comments

Comments
 (0)