I. Title Description
Given words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
? One letter can is changed at a time
? Each intermediate word must exist in the dictionary
For example, Given:
Start = "hit"
End = "COG"
Dict = ["Hot", "dot", "dog", "Lot", "log"]
As one shortest transformation is ' hit ', "Hot", "dot", "dog" and "cog", return its length 5.
Note:
? Return 0 If there is no such transformation sequence.
? All words has the same length.
? All words contain only lowercase alphabetic characters.
Two. Topic analysis
Reference Link: http://www.mamicode.com/info-detail-448603.html
This problem can be seen as a graph. We map the topic to the graph, the vertices are each string, and then the two strings are connected if one character is different. Our character set has only lowercase letters, and the length of the string is fixed, assuming L. Then you can notice that each character can have 25 sides (26 lowercase letters to remove itself), then a string may exist side is 25*l bar. The next step is to check that the corresponding strings are in the dictionary and get a complete picture of the structure. According to the requirements of the topic, equivalent to the shortest path of one vertex to another in this graph, we usually use BFS breadth first.
This problem, we can only use the simplest way to do, each time to change the word of a letter, and then gradually search, this shortest path, tree minimum depth problem with BFS most suitable.
The word adjacent to the current word, which is another vertex that is the same as the vertex, is a word that changes a letter to the current word and exists within the dictionary.
Find the next word of a word, join the BFS queue, we want to remove from the dictionary, because do not delete will cause similar hog->hot->hog such a dead loop. And the deletion of the shortest path has no effect, because we first found the word is definitely the shortest path, we are the sequence traversal to search, the first to find must be the shortest path, even if the other words can be converted to it, the path will certainly not be shorter than the current path. This problem only requires the shortest path length, no need to require the shortest path to output, so you can delete the word.
BFS queue with empty string "" to mark the interval between layers and layers, each hit the end of the layer, traverse depth +1, into the next layer.
Three. Sample code
classSolution { Public:intLadderlength (stringStartstringEnd unordered_set<string>&dict) {if(start.size () = =0|| End.size () = =0)return 0; Queue<string>WORDQ; Wordq.push (start); Wordq.push ("");intPath =1; while(!wordq.empty ()) {stringstr = Wordq.front (); Wordq.pop ();if(str! ="") {intLen = Str.size (); for(inti =0; i < Len; i++) {CharTMP = Str[i]; for(Charc =' A '; C <=' Z '; C + +) {if(c = = tmp)Continue; Str[i] = c;if(str = = end)returnPath +1;//If the changed word equals end returns path+1 if(Dict.find (str)! = Dict.end ()) {Wordq.push (str); Dict.erase (str);//Dictionary Delete this word to prevent repeated walking}} Str[i] = tmp;//Reset back to the original word} }Else if(!wordq.empty ()) {//arrives at the end of the current layer and is not the end of the last layerpath++; Wordq.push (""); } }return 0; }};
Leetcode notes: Word Ladder