Word Ladder leetcode java


題目

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be 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 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.

 

題解

 這道題是套用BFS同時也利用BFS能尋找最短路徑的特性來解決問題。

 把每個單詞作為一個node進行BFS。當取得當前字符串時,對他的每一位字符進行從a~z的替換,如果在字典里面,就入隊,並將下層count++,並且為了避免環路,需把在字典里檢測到的單詞從字典里刪除。這樣對於當前字符串的每一位字符安裝a~z替換后,在queue中的單詞就作為下一層需要遍歷的單詞了。

 正因為BFS能夠把一層所有可能性都遍歷了,所以就保證了一旦找到一個單詞equals(end),那么return的路徑肯定是最短的。

 

 像給的例子 start = hit,end = cog,dict = [hot, dot, dog, lot, log]

 按照上述解題思路的走法就是:

  level = 1    hit   dict = [hot, dot, dog, lot, log]

         ait bit cit ... xit yit zit ,  hat hbt hct ... hot ... hxt hyt hzt ,  hia hib hic ... hix hiy hiz

 

  level = 2    hot  dict = [dot, dog, lot, log]

         aot bot cot dot ...  lot ... xot yot zot,hat hbt hct ... hxt hyt hzt,hoa hob hoc ... hox hoy hoz

 

  level = 3    dot lot  dict = [dog log]

         aot bot ... yot zot,dat dbt ...dyt dzt,doa dob ... dog .. doy doz,

         aot bot ... yot zot,lat lbt ... lyt lzt,loa lob ... log... loy loz

 

  level = 4   dog log dict = []

         aog bog cog

 

  level = 5   cog  dict = []

 

 代碼如下:

 

 1      public  int ladderLength(String start, String end, HashSet<String> dict) {
 2          if(start== null || end== null || start.length()==0 || end.length()==0 || start.length()!=end.length())  
 3          return 0; 
 4         
 5         LinkedList<String> wordQueue =  new LinkedList<String>();
 6          int level = 1;  // the start string already count for 1
 7           int curnum = 1; // the candidate num on current level
 8           int nextnum = 0; // counter for next level
 9          
10         wordQueue.add(start);
11         
12          while(!wordQueue.isEmpty()){
13             String word = wordQueue.poll();
14             curnum--;
15             
16              for( int i = 0; i < word.length(); i++){
17                  char[] wordunit = word.toCharArray();
18                  for( char j = 'a'; j <= 'z'; j++){
19                     wordunit[i] = j;
20                     String temp =  new String(wordunit);  
21                     
22                      if(temp.equals(end))
23                          return level+1;
24                      if(dict.contains(temp)){
25                         wordQueue.add(temp);
26                         nextnum++;
27                         dict.remove(temp);
28                     }
29                 }
30             }
31             
32              if(curnum == 0){
33                 curnum = nextnum;
34                 nextnum = 0;
35                 level++;
36             }
37         }
38         
39          return 0;
40     }

                     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM