public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if (wordList.size() == 0) {
Set<String> dict = new HashSet<>();
for (String word : wordList) {
if (!dict.contains(endWord)) {
Set<String> visited = new HashSet<>();
Set<String> begin = new HashSet<>();
Set<String> end = new HashSet<>();
while (!begin.isEmpty() && !end.isEmpty()) {
if (begin.size() > end.size()) {
Set<String> temp = new HashSet<>();
for (String word : begin) {
char[] letters = word.toCharArray();
for (int i = 0; i < letters.length; i++) {
for (char c = 'a'; c <= 'z'; c++) {
String nextWord = new String(letters);
if (end.contains(nextWord)) {
if (dict.contains(nextWord) && !visited.contains(nextWord)) {