Skip to content

Updated WordLadderTest.java & WordLadder.java #3674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions src/main/java/com/thealgorithms/strings/WordLadder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,8 @@
beginWord != endWord
All the words in wordList are unique.
*/
class WordLadder {

/**
* Driver Code
*/
public static void main(String[] args) {
String beginWord = "hit";
String endWord = "cog";
String words[] = { "hot", "dot", "dog", "lot", "log", "cog" };
List<String> wordList = Arrays.asList(words);

System.out.println(
"Ladder Length: " + ladderLength(beginWord, endWord, wordList)
);
}
class WordLadder {

/**
* This function finds the ladderLength
Expand All @@ -60,11 +47,7 @@ public static void main(String[] args) {
* @return ladderLength: This function will return the ladderLength(level)
* if the endword is there. Otherwise, will return the length as 0.
*/
public static int ladderLength(
String beginWord,
String endWord,
List<String> wordList
) {
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
HashSet<String> set = new HashSet();
for (String word : wordList) {
set.add(word);
Expand Down
31 changes: 26 additions & 5 deletions src/test/java/com/thealgorithms/strings/WordLadderTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import java.util.*;

public class WordLadderTest {

@Test
public void testWordLadder() {
String words1[] = { "hot", "dot", "dog", "lot", "log", "cog" };
assertEquals(5, WordLadder.ladderLength("hit", "cog", Arrays.asList(words1)));
String words2[] = { "hot", "dot", "dog", "lot", "log" };
assertEquals(0, WordLadder.ladderLength("hit", "cog", Arrays.asList(words2)));

/**
* Test 1:
* Input: beginWord = "hit", endWord = "cog", wordList =
* ["hot","dot","dog","lot","log","cog"]
* Output: 5
* Explanation: One shortest transformation sequence is
* "hit" -> "hot" -> "dot" -> "dog" -> cog"
* which is 5 words long.
*/

List<String> wordList1 = Arrays.asList("hot", "dot", "dog", "lot", "log", "cog");
assertEquals(WordLadder.ladderLength("hit", "cog", wordList1), 5);

/**
* Test 2:
* Input: beginWord = "hit", endWord = "cog", wordList =
* ["hot","dot","dog","lot","log"]
* Output: 0
* Explanation: The endWord "cog" is not in wordList,
* therefore there is no valid transformation sequence.
*/

List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);

}
}