-Dictionary Problem (Java language) The word ladder game was invented by Lewis C
ID: 3592300 • Letter: #
Question
-Dictionary Problem (Java language)
The word ladder game was invented by Lewis Carroll in 1877. The idea is to being with a start word
and then change one letter at a time until you arrive at the end word. Each word along the way must be
an English word.
For example, starting from FISH, you can make the following word ladder to MAST:
FISH, WISH, WASH, MASH, MAST
Write a recursive program to find the word ladder given a start word and an end word, or determines
whether no word ladder exists. Use the
words.txt
file (provided) as your dictionary of valid words.
Your program doesn’t need to find the shortest word ladder, any ladder will work if one exists.
Explanation / Answer
class WordNode{ String word; int numSteps; public WordNode(String word, int numSteps){ this.word = word; this.numSteps = numSteps; } } public class Solution { public int ladderLength(String beginWord, String endWord, Set wordDict) { LinkedList queue = new LinkedList(); queue.add(new WordNode(beginWord, 1)); wordDict.add(endWord); while(!queue.isEmpty()){ WordNode top = queue.remove(); String word = top.word; if(word.equals(endWord)){ return top.numSteps; } char[] arr = word.toCharArray(); for(int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.