[Leetcode] [JavaScript] Word Ladder

Source: Internet
Author: User

https://leetcode.com/problems/word-ladder/

Word Ladder

Given words (beginword and Endword), and a dictionary, find the length of shortest transformation se Quence from Beginword to Endword, such that:

    1. One letter can is changed at a time
    2. 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 "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.

Sure enough JS is not suitable to do the problem, the lack of a variety of common methods.

This question hung countless times ah, at the beginning of the mistake into the parameter as an array, is actually a set, and then has been timed out ...

The idea of BFS is correct, but first can not construct the diagram, the Complexity O (n^2) certainly not.

Then try to find it directly in set, find a delete, and also timeout.

In the end, it can only be replaced by the present way.

The test case has a special character, dict words are not particularly long, but the amount of data is very large.

It eventually changed to an element in the BFS queue, replacing each letter with a-Z and then comparing it to the dictionary.

1 /**2 * @param {string} Beginword3 * @param {string} Endword4 * @param {set<string>} worddict5 * @return {number}6  */7 varLadderlength =function(Beginword, Endword, worddict) {8     varQueue = [];9     vari = 0;Ten Queue.push (Beginword); OneWorddict.Delete(Beginword); A     if(Onechardiff (Beginword, Endword) &&Worddict.has (Endword)) { -         return2; -}Else{ the         returnBFS (); -     } -      -     functionBFs () { +         vardepth = 1; -          while(Queue.length > 0){ +depth++; A             varCount =queue.length; at              while(count--){ -                 varCurr =Queue.shift (); -                 if(Onechardiff (Curr, Endword) && Curr!==Beginword) { -                     returndepth; -                 } -                 varNeedremove = []; in                  for(vari = 0; i < curr.length; i++){ -                      for(varj = ' A '. charCodeAt (); J <= ' Z '. charCodeAt (); J + +){ to                         varTestmatch =Curr; +                         varCH =String.fromCharCode (j); -                         if(Testmatch[i]!==ch) { theTestmatch =Replacechat (Testmatch, I, ch); *                         } $                         if(Worddict.has (Testmatch)) {Panax Notoginseng Queue.push (testmatch); -Worddict.Delete(Testmatch); the                         } +                     } A                 } the             } +         }         -          $         return0; $     } -     functionIsstrinarr (str, arr) { -          for(varIincharr) { the             if(str = = =Arr[i]) { -                 return true;Wuyi             } the         } -         return false; Wu     } -     functionreplacechat (source, POS, Newchar) { About         varSfrontpart = source.substr (0, POS);  $         varStailpart = SOURCE.SUBSTR (pos + 1, source.length);  -         returnSfrontpart + Newchar +Stailpart;  -     }   -     functionOnechardiff (A, b) { A         if(A.length!==b.length) { +             return false; the         } -         varCount = 0; $          for(vari = 0; i < a.length; i++){ the             if(A[i].tolowercase ()!==b[i].tolowercase ()) { thecount++; the             } the             if(Count >= 2){ -                 return false; in             } the         } the         if(Count = = 1){ About             return true; the}Else{ the             return false; the         } +     } -};

[Leetcode] [JavaScript] 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.