Intuition:

Strategy:

Example Walk-through

WordLadder.pdf

WordLadder.pptx

Algorithm

def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        # generate list of neighbors
        def getPotentialNeighbors(path:str)-> List[str]:
            neighborList=[]
            pathArr = list(path)
            
            for i in range(len(path)):                
                
                for letter in alphabet:                                    
                    tmp = path[i]  
                    
                    pathArr[i] = letter
                    neighborList.append(''.join(pathArr))
                    
                    pathArr[i] = tmp
            
            return neighborList
        
        seen = set()
        wordSet = set()
        level = 0
        alphabet = "abcdefghijklmnopqrstuvwxyz"
        
        for word in wordList:
            wordSet.add(word)            
        
        q = []        
        q.append(beginWord)
        
        if endWord not in wordList:
            return 0
        while len(q)>0:            
            
            levelSize = len(q)
            level += 1
            
						# LevelOrder: 
						#   Only loop through current level's nodes, 
						#   while adding more to the back of queue
            for i in range(levelSize):  
                cur = q.pop(0)
                if cur == endWord:
                    return level
                
                neighbors = getPotentialNeighbors(cur)
                for neigh in neighbors:
                    if neigh in wordSet and neigh not in seen:
                        q.append(neigh)
                        seen.add(neigh)

        return 0