#### 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:

• Only one letter can be changed at a time.
• Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

```Example: input="hit",output="cog" wordList = ["hot","dot","dog","lot","log","cog", "hip"] Output: 5 As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5.```

This is classic BFS(bread first search) problem. Imagine building a tree with root node `hit` and then it's child nodes being other words created by replacing only one character from the parent node. In this case child nodes of `hit` will be `"hot", "hip"`.

Continue finding child node for `"hit", "hip"`.

```
"hit"
/   \
"hot" "hip"
/   \
"dot" "lot"
/      /
"dog"   "log"
/       /
"cog"   "cog"```

The solution for the above problem

`````` public int ladderLength(String beginWord, String endWord,
Set<String> wordList) {
WordCount wc = new WordCount(beginWord, 1);
while (!q.isEmpty()) {
WordCount w = q.poll();
if (w.word.equals(endWord))
return w.count;
for (int i=0;i< w.word.length(); i++) {
for(int j=0;j<26;j++) {
char[] arr = w.word.toCharArray();
arr[i] =(char) (j+'a');
String s = new String(arr);
if (wordList.contains(s)) {
wordList.remove(s);
}
}
}
}
return 0;
}

class WordCount {
public String word;
public int count;

public WordCount(String word, int count) {
this.word = word;
this.count = count;
}
}``````