BFS的变形体。对起始单词的每一个字母都做替换,把替换后的单词加入到queue中,并同时维护一个Map来记录变化前单词和变化后单词的联系。用来回溯时能打印出路径。
package Hard;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Given two words of equal length that are in a dictionary, write a method to trans-
form one word into another word by changing only one letter at a time. The new
word you get in each step must be in the dictionary.
给定两个有着相同长度且都在字典内的单词,要求写一个方法来把一个单词变型成另一个单词。
一次只能转换一个字母,且每次生成的单词必须在字典内
*
*/
public class S18_10 {
public static LinkedList<String> transform(String startWord, String stopWord, Set<String> dictionary) {
startWord = startWord.toUpperCase();
stopWord = stopWord.toUpperCase();
Queue<String> actionQueue = new LinkedList<String>();
Set<String> visitedSet