Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static ArrayList<String> getWordLadderDFS(String start, String end) {
- ArrayList<String> wordLadder = new ArrayList<String>();
- Set<String> dict = makeDictionary();
- dict.remove(start); //remove the start word to say this node has been visited
- ArrayList<String> branches = new ArrayList<String>(); //the arrayList containing all the branches of the current word
- for(String i: dict){ //iterate thru the dictionary and add all words that differ by one letter from the start
- if(oneLetterDiff(start, i))
- branches.add(i);
- }
- if(branches.size() == 0) //if there are no next words, then there is no ladder
- return new ArrayList<String>();
- if(branches.contains(end)){ //if the branch contains the last word already, return an array of size two
- ArrayList<String> ret = new ArrayList<String>();
- ret.add(start);
- ret.add(end);
- return ret;
- }
- return wordLadder;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement