leetCode127 . Word Ladder Java Edition (word isometric transformation)

Reprint address: https://blog.csdn.net/mine_song/article/details/68951974

127. Become Ladder

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord toendWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note thatbeginWord isnot a transformed word.

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

Shortest path in undirected graph - level order in BFS

Given two words (start and end) and a dictionary, find the shortest sequence of transitions from start to end

for example:

  1. Only change one letter at a time.
  2. Intermediate words in the transformation process must appear in the dictionary.
Thought: At this time, a bfs question began to suddenly fail to respond. It can be imagined as a tree, the root node is the start string, and the second layer is all strings that differ from it by one letter (the ones that have appeared before, there is no need to appear after that, because if they appear, it is also abc becomes bbc and then Change back to abs, meaningless), use a hashmap to save the layer number of each node, and also need a queue to implement breadth-first search, because it is traversed from the top to the bottom, so it is found when it is equal to end The value of the layer is the smallest, and you can return it.

[java]  view plain copy  
  1. import  java.util. *;  
  2. publicclass word {   
  3.     publicint ladderLength(String beginWord, String endWord, List<String> wordList) {   
  4.         if (beginWord == null || endWord == null || beginWord.length() == 0 || endWord.length() == 0  
  5.                 || beginWord.length() != endWord.length())  
  6.             return0;   
  7.         // The key to this question is to remove duplicates, and to remove the same word as beginWord  
  8.         Set<String> set = new HashSet<String>(wordList);  
  9.         if (set.contains(beginWord))  
  10.             set.remove(beginWord);  
  11.         Queue<String> wordQueue = new LinkedList<String>();  
  12.         int level = 1// the start string already count for 1  
  13.         int curnum = 1;// the candidate num on current level  
  14.         int nextnum = 0;// counter for next level  
  15.         // 或者使用map记录层数  
  16.         // Map<String, Integer> map = new HashMap<String, Integer>();  
  17.         // map.put(beginWord, 1);  
  18.         wordQueue.add(beginWord);  
  19.   
  20.         while (!wordQueue.isEmpty()) {  
  21.             String word = wordQueue.poll();  
  22.             curnum--;  
  23.             // int curLevel = map.get(word);  
  24.             for (int i = 0; i < word.length(); i++) {  
  25.                 char[] wordunit = word.toCharArray();  
  26.                 for (char j = 'a'; j <= 'z'; j++) {  
  27.                     wordunit[i] = j;  
  28.                     String temp = new String(wordunit);  
  29.   
  30.                     if (set.contains(temp)) {  
  31.                         if (temp.equals(endWord))  
  32.                             // return curLevel + 1;  
  33.                             return level + 1;  
  34.                         // map.put(temp, curLevel + 1);  
  35.                         nextnum++;  
  36.                         wordQueue.add(temp);  
  37.                         set.remove(temp);  
  38.                     }  
  39.                 }  
  40.             }  
  41.             if (curnum == 0) {  
  42.                 curnum = nextnum;  
  43.                 nextnum = 0;  
  44.                 level++;  
  45.             }  
  46.         }  
  47.         return0;   
  48.     }  
  49. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325584163&siteId=291194637