Word Ladder

1. Title

Word Ladder

2.   Http address

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

3. The question

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

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

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

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

4. My code (AC)

 1 import java.util.*;
 2 public class WordLadder {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6 
 7     }
 8     // Accepted
 9        public int ladderLength(String beginWord, String endWord, Set<String> wordDict) {
10             
11            if ( beginWord == null || beginWord.equals(endWord))
12                    return 1;
13            Queue<String> currQue = new LinkedList<String>();
14            HashSet<String> used = new HashSet<String>();
15            currQue.add(beginWord);
16            used.add(beginWord);
17            int count = 1;
18            while( !currQue.isEmpty())
19            {
20                Queue<String> nextQue = new LinkedList<String>();
21                count++;
22                 while( !currQue.isEmpty())
23                 {
24                     String word = currQue.poll();
25                     for(String transform : getTransform(word, used, wordDict,endWord))
26                     {
27                         if (transform.equals(endWord))
28                             return count;
29                         nextQue.add(transform);
30                     }
31                     
32                 }
33                 
34                 currQue = nextQue;
35            }
36            return 0;
37         
38        }
39            
40        public List<String> getTransform(String word, HashSet<String> used, Set<String> dict, String endWord)
41        {
42            List<String> res = new ArrayList<String>();
43            char [] letters = word.toCharArray();
44            for( int i = 0 ; i < letters.length; i++)
45            {
46                char original = letters[i];
47                for( char c = 'a' ; c <= 'z' ; c++)
48                {
49                    if ( c != original)
50                    {
51                        letters[i] = c;
52                        String trans = new String(letters);
53                        if ( trans.endsWith(endWord) || (!used.contains(trans) && dict.contains(trans)) ){
54                            res.add(trans);
55                            used.add(trans);
56                        }
57                    }
58                }
59                
60                letters[i] = original;
61            }
62            return res;
63        }
64 }    
原文地址:https://www.cnblogs.com/ordili/p/4928295.html