Leetcode Word Ladder

Source: Internet
Author: User

Given two strings start and end, and a string dictionary dict, judging from start to end after a few changes, each change can only take the word in the dictionary, and the adjacent changes only one character is different. For example:

For example,

Given:
Start ="hit"
End ="cog"
dict =["hot","dot","dog","lot","log"]

As one shortest transformation "hit" -> "hot" -> "dot" -> "dog" -> "cog" is,
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.
Thought, this question thought for a long while did not think of any good method. Let's get a direct AC approach. Similar to the output level order topic, each layer of each layer of consideration, that is so-called BSF. Tell us that only lowercase letters actually have meaning. It's just that every time you go through changing a letter, consider the lower case. With start as a number, its child is a string that is 1 apart from him and then determines if these can be changed by changing a character to end. If you can return the distance. If it is not possible to determine whether the string after the change of a character in DiC, then add to the que, que with "" "empty string separation, before we do level order is null because the que is the root node, where the string is stored, so with the null character. Encounter null character if que is already empty then you can return 0, if not empty, then push the null character to represent another layer.
classSolution { Public:intLadderlength (stringStartstringEnd, unordered_set<string> &dict) {    if(dict.size () = =0|| start = = end)return 0; if(Start.size ()! = End.size ())return 0; Queue<string>que; Que.push (start); Que.push (""); intdis =1;  while(!Que.empty ()) {        stringstr =Que.front ();        Que.pop (); if(str! ="")        {             for(inti =0; I < str.size (); ++i) { for(Charj ='a'; J <'Z'; ++j) {if(j = = Str[i])Continue; CharTMP =Str[i]; Str[i]=J; if(str = = end)returnDis +1; if(Dict.count (str) >0) {dict.erase (str);                    Que.push (str); } Str[i]=tmp; }            }        }        Else if(!Que.empty ()) {Que.push (""); Dis++; }    }    return 0;}};

The above practice requires that strings in the push to que be deleted in dic, otherwise the loop will die.

Online there are two que, but also swap, feel more trouble than the above. But the idea should be the same.

Leetcode Word Ladder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.