public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        HashSet<String> hs = new HashSet<>();
        Queue<String> queue = new LinkedList<>();
        wordList.add(endWord);
        queue.add(beginWord);
        int length = 1;
        while (!queue.isEmpty()) {
            length++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                String word = queue.poll();
                for (String newWord : getCandidates(hs, word, wordList)) {
                    if (newWord.equals(endWord)) {
                        return length;
                    }
                    queue.add(newWord);
                    hs.add(newWord);
                }
            }
        }
        return 0;
    }