mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +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
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function finds the ladderLength
|
||||
*
|
||||
@ -60,11 +47,7 @@ class WordLadder {
|
||||
* @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);
|
||||
|
Reference in New Issue
Block a user