I have a class that I'm using to find the shortest word ladder between two words. Each "Step" in that ladder is returned as a String element in a LinkedList. If it can't find one, it returns an empty LinkedList. The class is only intended to work with capital letters.

A step can only be formed from a word in the HashSet<String> dictionary variable.

I haven't made anything like this before, and I'm posting here because I'd appreciate help making it run faster and advice regarding how to make the code itself more easily read by other humans.

public class WordLadder {

private HashSet<String> dictionary;

this.dictionary = dictionary;
}

/**
* This method finds the shortest word ladder between two words and returns
* it as a LinkedList of Strings.
*
*/
@SuppressWarnings("unchecked")

// this method relies on deleting entries from a HashSet
HashSet<String> tempDict = (HashSet<String>) this.dictionary.clone();

// split the input line into the starting and ending words
String startWord = input.substring(0, input.indexOf(" "));
String endWord = input.substring(input.indexOf(" ") + 1);

// holds the shortest ladder found

while (!words.isEmpty()) {
Integer distance = distances.pop();
String word = words.pop();

// has the shortest ladder been found?
if (word.equals(endWord)) {
}

for (int i = 0; i < word.length(); i++) {

char[] currCharArr = word.toCharArray();

for (char c = 'A'; c <= 'Z'; c++) {
currCharArr[i] = c;

String newWord = new String(currCharArr);

if (tempDict.contains(newWord)) {
tempDict.remove(newWord);
}
}
}
}

}

}


My few problems:

1. Instead HashSet<String> tempDict = (HashSet<String>) this.dictionary.clone(); you could simply use Set<String> tempDict = new HashSet<>( this.dictionary );. You will avoid ugly @SuppressWarnings("unchecked").
2. Parsing input could be performed in other method while this one would accept two strings: initial and final word. That way it's much more versatile like Sharon Ben Asher mentioned.
3. Code most likely works but you just can't see what is really doing. Either commenting or extracting vital parts of algorithm into named methods would most likely shed some light.

I would really suggest trying to implement graph based algorithm where nodes are words and edges connect words with distance of 1. That way you could construct Graph once and use for example Dijkstra's algorithm to determine shortest path between two given nodes if it exists. Complexity would be O(n^2) where n is number of words. It could be a bit more intuitive with a bit of an explanation and possibly faster on multiple runs.

Fist of all, the code doesn't work. here's my test method. I get an empty response.

public static void main(String... args) {

HashSet<String> dictionary = new HashSet<>();


2. code to the interface: if you meant words to be a LIFO stack, use the Stack interface of push() and pop(). there are two reason for that: one is that it makes the usage of words clearer to another human. more importantly - how do you that LinkedList add() is doing the same as push()?
• all the more reason to use the proper interface. queue does not have pop(). – Sharon Ben Asher May 7 at 13:53