mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Update WordLadderTest.java & WordLadder.java (#3674)
This commit is contained in:

committed by
GitHub

parent
2e25f89c36
commit
a0d03e814a
@ -34,22 +34,9 @@ import java.util.Queue;
|
|||||||
beginWord != endWord
|
beginWord != endWord
|
||||||
All the words in wordList are unique.
|
All the words in wordList are unique.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class WordLadder {
|
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function finds the ladderLength
|
* This function finds the ladderLength
|
||||||
*
|
*
|
||||||
@ -60,11 +47,7 @@ class WordLadder {
|
|||||||
* @return ladderLength: This function will return the ladderLength(level)
|
* @return ladderLength: This function will return the ladderLength(level)
|
||||||
* if the endword is there. Otherwise, will return the length as 0.
|
* if the endword is there. Otherwise, will return the length as 0.
|
||||||
*/
|
*/
|
||||||
public static int ladderLength(
|
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
|
||||||
String beginWord,
|
|
||||||
String endWord,
|
|
||||||
List<String> wordList
|
|
||||||
) {
|
|
||||||
HashSet<String> set = new HashSet();
|
HashSet<String> set = new HashSet();
|
||||||
for (String word : wordList) {
|
for (String word : wordList) {
|
||||||
set.add(word);
|
set.add(word);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -9,9 +8,31 @@ public class WordLadderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWordLadder() {
|
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" };
|
* Test 1:
|
||||||
assertEquals(0, WordLadder.ladderLength("hit", "cog", Arrays.asList(words2)));
|
* 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user