@@ -0,0 +1,2423 @@
+/*%0A 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:%0A%0A    Only one letter can be changed at a time%0A    Each intermediate word must exist in the word list%0A%0AFor example,%0A%0AGiven:%0AbeginWord = %22hit%22%0AendWord = %22cog%22%0AwordList = %5B%22hot%22,%22dot%22,%22dog%22,%22lot%22,%22log%22%5D%0A%0AAs one shortest transformation is %22hit%22 -%3E %22hot%22 -%3E %22dot%22 -%3E %22dog%22 -%3E %22cog%22,%0Areturn its length 5.%0A%0ANote:%0A%0A    Return 0 if there is no such transformation sequence.%0A    All words have the same length.%0A    All words contain only lowercase alphabetic characters.%0A%0A*/%0A%0Aimport java.util.*;%0A%0Apublic class WordLadder %7B%0A%09public static int ladderLength(String beginWord, String endWord, Set%3CString%3E wordList) %7B%0A        if(beginWord.length() != endWord.length()) return 0;%0A        int res = 1;%0A        Queue%3CString%3E q = new LinkedList%3CString%3E();%0A        q.offer(beginWord);%0A        q.offer(null);%0A        HashSet%3CString%3E hs = new HashSet%3C%3E();%0A        hs.add(beginWord);%0A        while(!q.isEmpty())%7B%0A            String a = q.poll();%0A            if(a != null)%7B%0A                for(int i=0; i%3Ca.length();i++)%7B%0A                    char%5B%5D chars = a.toCharArray();%0A                    for(int j = 0; j %3C 26; j++)%7B%0A                        chars%5Bi%5D = (char)(j + 'a');%0A                        String word = new String(chars);%0A                        if(word.equals(endWord)) return res+1;%0A                        if(hs.contains(word)) continue;%0A                        if(wordList.contains(word))%7B%0A                            q.offer(word);%0A                            hs.add(word);%0A                        %7D%0A                    %7D%0A                %7D%0A            %7Delse%7B%0A                res++;%0A                if(q.isEmpty()) break;%0A                q.offer(null);%0A            %7D%0A        %7D%0A        return 0;%0A    %7D  %0A%0A   %09public static void main(String%5B%5D args) %7B%0A%09%09String%5B%5D wordList = new String%5B%5D%7B%22hot%22,%22dot%22,%22dog%22,%22lot%22,%22log%22%7D;%0A%09%09String beginWord = %22hit%22;%0A%09%09String endWord = %22cog%22;%0A%09%09HashSet%3CString%3E hl = new HashSet%3C%3E();%0A%09%09for(int i=0; i %3C wordList.length; i++)%7B%0A%09%09%09hl.add(wordList%5Bi%5D);%0A%09%09%09System.out.print(wordList%5Bi%5D + %22 %22);%0A%09%09%7D%0A%09%09System.out.println();%09%09%0A%09%09int res = ladderLength(beginWord, endWord, hl);%0A        System.out.println(%22Begin word: %22 + beginWord);%0A        System.out.println(%22End word: %22 + endWord);%0A        System.out.println(%22Distance: %22 + res);%0A%09%09return;%0A%09%7D%09%0A%7D%0A

word ladder