Copy public class Solution {
public int ladderLength ( String beginWord , String endWord , List < String > wordList) {
if (wordList == null || wordList . size () == 0 ) return 0 ;
if ( beginWord . equals (endWord)) return 1 ;
Set < String > dict = new HashSet < String >(wordList);
dict . add (beginWord);
Set < String > visitedSet = new HashSet < String >();
visitedSet . add (beginWord);
Queue < String > queue = new LinkedList < String >();
queue . offer (beginWord);
int count = 1 ;
while ( ! queue . isEmpty ()) {
count ++ ;
int size = queue . size ();
for ( int i = 0 ; i < size; i ++ ) {
String word = queue . poll ();
for ( String nextWord : getNextWord(word , dict) ) {
if ( visitedSet . contains (nextWord)) continue ;
if ( nextWord . equals (endWord)) return count;
visitedSet . add (nextWord);
queue . offer (nextWord);
}
}
}
return 0 ;
}
private List < String > getNextWord ( String word , Set < String > dict) {
List < String > nextWordList = new ArrayList <>();
for ( int i = 0 ; i < word . length (); i ++ ) {
for ( char c = 'a' ; c <= 'z' ; c ++ ) {
if (c == word . charAt (i)) continue ;
String nextWord = replace(word , i , c) ;
if ( dict . contains (nextWord)) nextWordList . add (nextWord);
}
}
return nextWordList;
}
private String replace ( String word , int index , char c) {
char [] strArr = word . toCharArray ();
strArr[index] = c;
return new String(strArr) ;
}
}
Copy for ( char c = 'a' ; c <= 'z' ; c ++ ) {
for ( int i = 0 ; i < word . length (); i ++ ) {
if (c == word . charAt (i)) {
continue ;
}
String nextWord = replace(word , i , c) ;
if ( wordList . contains (nextWord)) {
nextWordList . add (nextWord);
}
}
}
Copy if ( nextWord . equals (endWord)) {
return count;
}