(begin_word, end_word, word_list)
return -1
Bidirectional BFS!!!
    :type begin_word: str
    :type end_word: str
    :type word_list: Set[str]
    :rtype: int
Bidirectional BFS!!!
    :type begin_word: str
    :type end_word: str
    :type word_list: Set[str]
    :rtype: int
Bidirectional BFS!!! : type begin_word : str : type end_word : str : type word_list : Set [ str ] : rtype : int
def ladder_length(begin_word, end_word, word_list):
    """
    Bidirectional BFS!!!
    :type begin_word: str
    :type end_word: str
    :type word_list: Set[str]
    :rtype: int
    """
    if len(begin_word) != len(end_word):
        return -1   # not possible

    if begin_word == end_word:
        return 0

    # when only differ by 1 character
    if sum(c1 != c2 for c1, c2 in zip(begin_word, end_word)) == 1:
        return 1

    begin_set = set()
    end_set = set()
    begin_set.add(begin_word)
    end_set.add(end_word)
    result = 2
    while begin_set and end_set:

        if len(begin_set) > len(end_set):
            begin_set, end_set = end_set, begin_set

        next_begin_set = set()
        for word in begin_set:
            for ladder_word in word_range(word):
                if ladder_word in end_set:
                    return result
                if ladder_word in word_list:
                    next_begin_set.add(ladder_word)
                    word_list.remove(ladder_word)
        begin_set = next_begin_set
        result += 1
        # print(begin_set)
        # print(result)
    return -1
def ladder_length ( begin_word , end_word , word_list ) : if len ( begin_word ) != len ( end_word ) : return - 1 # not possible if begin_word == end_word : return 0 # when only differ by 1 character if sum ( c1 != c2 for c1 , c2 in zip ( begin_word , end_word ) ) == 1 : return 1 begin_set = set ( ) end_set = set ( ) begin_set . add ( begin_word ) end_set . add ( end_word ) result = 2 while begin_set and end_set : if len ( begin_set ) > len ( end_set ) : begin_set , end_set = end_set , begin_set next_begin_set = set ( ) for word in begin_set : for ladder_word in word_range ( word ) : if ladder_word in end_set : return result if ladder_word in word_list : next_begin_set . add ( ladder_word ) word_list . remove ( ladder_word ) begin_set = next_begin_set result += 1 # print(begin_set) # print(result) return - 1