Issue
In the else statement of this main for loop, I am trying to add each newly created stacks into my queue and need to keep going on over the same queue until it's empty or I find the end word.
for (Stack<String> next:wordQ){
if(next.peek().toString().equals(start)){
for(String each:next){
ladder.add(each);
return ladder;
}
}
else {
LinkedList<String> temp2 = new LinkedList<String>();
temp2 = (LinkedList<String>) getWordsOneAway(next.peek().toString());
for ( String nextWord:temp2){
@SuppressWarnings("unchecked")
Stack<String> nextTempStack = (Stack<String>) next.clone();
nextTempStack.push(nextWord);
wordQ.add(nextTempStack);
}
buildLadder(start, next.peek().toString());
}
}
Tried using Iterator. Same issue.
Iterator<Stack<String>> myQIter = wordQ.iterator();
while ( myQIter.hasNext()){
Stack<String> tempStack = myQIter.next();
//System.out.println("This is peek: " +tempStack.peek());
if(tempStack.peek().toString().equals(start)){
for(String each:tempStack){
ladder.add(each);
return ladder;
}
}
else {
LinkedList<String> temp2 = new LinkedList<String>();
temp2 = (LinkedList<String>) getWordsOneAway(tempStack.peek().toString());
for ( String nextWord:temp2){
@SuppressWarnings("unchecked")
Stack<String> nextTempStack = (Stack<String>) tempStack.clone();
nextTempStack.push(nextWord);
wordQ.add(nextTempStack);
}
buildLadder(start, tempStack.peek().toString());
}
}
wordQ.add(nextTempStack); This is the issue
Solution
Here is complete class. Got it to work without recursion.The old school for int i loop took care if concurrent modification problem.
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
* Class that created a word-ladder between two words (if possible)
* @author GJ
* @version 4/27/2015
*/
public class LadderBuilder {
private Collection<String> dictionary;
private Collection<String> usedWords = new HashSet<String>();
public LadderBuilder(Collection<String> dictionary) {
this.dictionary = dictionary;
}
public Deque<String> buildLadder(String start, String end){
LinkedList<String> temp = new LinkedList<String>();
Queue<Stack<String>> wordQ = new LinkedList<Stack<String>>();
Deque<String> ladder = new LinkedList<String>();
if ( start.length() != end.length()){
System.err.println("Selected words are not of the same length.");
System.exit(1);
}
temp = (LinkedList<String>) getWordsOneAway(end);
Iterator<String> myIter = temp.iterator();
while(myIter.hasNext()){
Stack<String> words = new Stack<>();
words.push(end);
words.push(myIter.next());
wordQ.add(words);
}
for (int i = 0; i < wordQ.size(); i++){
Stack<String> temp3 = wordQ.poll();
i--;
if(temp3.peek().equals(start)){
for(String each:temp3){
ladder.add(each);
}
return ladder;
}
else {
LinkedList<String> temp2 = new LinkedList<String>();
temp2 = (LinkedList<String>) getWordsOneAway(temp3.peek());
for ( String nextWord:temp2){
@SuppressWarnings("unchecked")
Stack<String> nextTempStack = (Stack<String>) temp3.clone();
nextTempStack.push(nextWord);
wordQ.add(nextTempStack);
}
}
}
return null;
}
private List<String> getWordsOneAway(String word){
usedWords.add(word);
List<String> oneAwayList = new LinkedList<String>();
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 (dictionary.contains(newWord) && !(usedWords.contains(newWord))) {
oneAwayList.add(newWord);
usedWords.add(newWord);
}
}
}
return oneAwayList;
}
}
Answered By - script_before_java
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.