HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Word Ladder Solution in Java using ArrayList

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javawordusingladdersolutionarraylist

Problem

Word Ladder


Given a start word and a goal word, convert the start word to the goal word by changing one letter at a time. Each step must also be a valid word.

Is my code modular? I mean are functions are concise, class design etc.? Could someone please review the code and let me know if code is good and if it has some design issues. Any tips on improving the design, and making the code cleaner, change variable names?

Used dictionary at this URL.

```
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 dic= new HashSet<>();
private final List nodeQ= new LinkedList<>();
private final HashSet 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");
//WordLadder wl= new WordLadder("stone","chore");
//WordLadder wl= new WordLadder("stone","choky");
//WordLadder wl= new WordLadder("charge","comedo"); //takes time ~ 3 mins- seems hardest

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- \n"+nodeQ);
System.out.println("visitedN

Solution

Style

-
Using braces {} for single if statements would make your code less errorprone. If you decide to not use them, you should be at least consistent with your style. You are mixing both.

-
give your variables some space to breathe.

while(s!=null){


will be much more readable like

while (s != null) {


Naming

  • reading getPaths() I would expect to get something back by calling this method.



  • you shouldn't shorten variable names. You are using a lot of single letter names which is ok for a loop iteration counter, but not like you use them. Improving readability will make Mr.Maintainer happy.



Node

  • You should make str final to show that it isn't changed anywhere.



-
In the hashCode() method you can remove the result variable and just return the result

public int hashCode() {
    final int prime = 31;
    return prime + ((str == null) ? 0 : str.hashCode());
}

Code Snippets

while(s!=null){
while (s != null) {
public int hashCode() {
    final int prime = 31;
    return prime + ((str == null) ? 0 : str.hashCode());
}

Context

StackExchange Code Review Q#78098, answer score: 5

Revisions (0)

No revisions yet.