The word ladder game was invented by Lewis Carroll in 1877. The idea is to begin
ID: 3694573 • Letter: T
Question
The word ladder game was invented by Lewis Carroll in 1877. The idea is
to begin with a start word and change one letter at a time until arriving at
an end word. Each word along the way must be an English word.
For example, starting from FISH you can make a word ladder to MAST
through the following ladder:
FISH, WISH, WASH, MASH, MAST
Write a program that uses recursion to find the word ladder given a start
word and an end word, or determines if no word ladder exists. Use the
file words.txt that is available online with the source code for the book
as your dictionary of valid words. This file contains 87314 words. Your
program does not need to find the shortest word ladder between words,
any word ladder will do if one exists.
This is from Walter Savitch, Java: An introduction to problem solving and programming. It's from Chapter 11, number 8. Please solve this recursively using relatively simple methods. This is a very simple program for an intro to Java course. It does not need any fancy techniques like nodes and regex and all that, it just needs to be done recursively. Chegg does not let me upload the words.txt file, but it can be found here: http://s000.tinyupload.com/index.php?file_id=16996529956442064272. Thank you.
Explanation / Answer
program:
package test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordLadder
{
private final HashSet<String> dic= new HashSet<>();
private final List<Node> nodeQ= new LinkedList<>();
private final HashSet<Node> visitedNodes= new HashSet<>();
private final String in;
private final String target;
public WordLadder(String i, String t)
{
in=i;
target= t;
}
public static void main(String[] args) throws IOException
{
WordLadder wl= new WordLadder("stone","money");
wl.loadDictionary();
if(!wl.dic.contains(wl.in)||!wl.dic.contains(wl.target)){
System.out.println("error words not in dic");
}
wl.nodeQ.add(new Node(wl.in));
wl.getPaths();
}
private void getPaths()
{
long st= System.currentTimeMillis();
while(!isMatchFound()){
Node n= selectNext();
nodeQ.remove(n);
addNextWordsToQ(n);
visitedNodes.add(n);
}
System.out.println("nodeQ- "+nodeQ);
System.out.println("visitedNodes- "+visitedNodes);
long end= System.currentTimeMillis();
System.out.println("time taken in sec~ "+ (end-st)/1000);
System.out.println("time taken in min~ "+ (end-st)/60000);
}
private Node selectNext()
{
Node sel= null;
int minMatch=-1;
int match;
for(Node n: nodeQ)
{
match=0;
for(int i=0; i<target.length(); i++)
{
if(n.str.charAt(i)== target.charAt(i))
{
match++;
}
}
if(match>minMatch)
{
sel=n;
minMatch=match;
}
}
return sel;
}
private void addNextWordsToQ(Node n)
{
String s= n.str;
for(int i=0;i<s.length();i++)
{
String regex= s.substring(0,i)+"."+s.substring(i+1);
Pattern p= Pattern.compile(regex);
for(String d: dic){
Matcher m= p.matcher(d);
if(!d.equals(s) && s.length()==d.length()
&& m.find() && !isNodeVisited(d))
{
nodeQ.add(new Node(d,n));
}
}
}
}
private boolean isMatchFound()
{
for(Node n: nodeQ)
{
if(target.equals(n.str))
{
System.out.println(n);
return true;
}
}
return false;
}
private boolean isNodeVisited(String add)
{
for(Node n: visitedNodes)
{
if(n.str.equals(add))
{
return true;
}
}
return false;
}
private void loadDictionary() throws IOException
{
InputStream is= WordLadder.class.getClassLoader().getResourceAsStream("dictionary.txt");
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String s= br.readLine();
while(s!=null){
dic.add(s);
s= br.readLine();
}
}
}
class Node
{
String str;
final List<String> path= new ArrayList<>();
public Node(String str)
{
this.str=str;
}
public Node(String str, Node parent)
{
this.str=str;
path.addAll(parent.path);
path.add(parent.str);
}
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((str == null) ? 0 : str.hashCode());
return result;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (str == null)
{
if (other.str != null)
return false;
}
else if (!str.equals(other.str))
return false;
return true;
}
public String toString()
{
return " " + str + ", " + path+ "";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.