Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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.
Note:

Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.
Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

My code:

import collections
class Solution(object):
    def ladderLength(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: List[str]
        :rtype: int
        """
        wordList.append(beginWord)
        wordList=set(wordList)
        #print(len(wordList))
        
        self.dict=collections.defaultdict(list)
        #first build begin word
        self.build_dict(beginWord,wordList)
        #exit()
        #for item in wordList:
        #    self.build_dict(item,wordList)
        #self.build_dict(endWord,wordList)
        count=1
        start_list=set([beginWord])
        checked_set=set()
        while start_list:
            tmp_list=set()
            for item in start_list:
                wordList.remove(item)
                self.build_dict(item,wordList)
                check_str_list=self.dict[item]
                for new_str in check_str_list:
                    
                    if new_str==endWord:
                        return count+1
                    
                    if new_str not in checked_set:
                        tmp_list.add(new_str)
                        checked_set.add(new_str)
            count+=1
            
            start_list=tmp_list
            #print(len(checked_set))
            #print(count)
        return 0
    def build_dict(self,beginWord,wordList):
        word_len=len(beginWord)
        for item in wordList:
            count=0
            for i in range(word_len):
                if beginWord[i]!=item[i]:
                    count+=1
            if count==1:
                self.dict[beginWord].append(item)
            
        
        

Solution: https://leetcode.com/problems/word-ladder/