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.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Graph traversal

Starter code: class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        

Solutions:
class Solution:

	def ladderLength(self, beginWord, endWord, wordList):
		wordDict = set(wordList)
		if not endWord in wordDict:
			return 0
		visited = set()
		beginSet = set()
		beginSet.add(beginWord)
		visited.add(beginWord)
		endSet = set()
		endSet.add(endWord)
		visited.add(endWord)
		lenWord = len(beginWord)
		distance = 1
		while len(beginSet) > 0 and len(endSet) > 0:
			if len(beginSet) > len(endSet):
				(beginSet, endSet) = (endSet, beginSet)
			newSet = set()
			for w in beginSet:
				for i in range(lenWord):
					part1 = w[:i]
					part2 = w[i + 1:]
					for alpha in 'abcdefghijklmnopqrstuvwxyz':
						target = part1 + alpha + part2
						if target in endSet:
							return distance + 1
						elif not target in visited and target in wordDict:
							newSet.add(target)
							visited.add(target)
			beginSet = newSet
			distance += 1
		return 0


class Solution:

	def ladderLength(self, beginWord, endWord, wordList):
		words = set(wordList)
		if endWord not in words:
			return 0
		(beginQ, endQ) = ({beginWord}, {endWord})
		dist = 2
		while beginQ:
			print(('beginQ', beginQ, 'endQ', endQ))
			newq = set()
			for u in beginQ:
				for i in range(len(u)):
					for c in 'abcdefghijklmnopqrstuvwxyz':
						v = u[:i] + c + u[i + 1:]
						if v in words:
							if v in endQ:
								return dist
							newq.add(v)
			dist += 1
			beginQ = newq
			if len(beginQ) > len(endQ):
				(beginQ, endQ) = (endQ, beginQ)
			words -= beginQ
		return 0


class Solution:

	def ladderLength(self, beginWord, endWord, wordDict):
		(front, back) = (set([beginWord]), set([endWord]))
		wordDict = set(wordDict)
		length = 2
		width = len(beginWord)
		charSet = 'abcdefghijklmnopqrstuvwxyz'
		if endWord not in wordDict:
			return 0
		while front:
			newFront = set()
			for phase in front:
				for i in range(width):
					for c in charSet:
						nw = phase[:i] + c + phase[i + 1:]
						if nw in back:
							return length
						if nw in wordDict:
							newFront.add(nw)
			front = newFront
			if len(front) > len(back):
				(front, back) = (back, front)
			wordDict -= wordDict & front
			length += 1
		return 0


class Solution:

	def ladderLength(self, beginWord, endWord, wordList):
		letters = 'abcdefghijklmnopqrstuvwxyz'
		wordList = set(wordList)
		if endWord not in wordList:
			return 0
		reached = set()
		backreached = set()
		reached.add(beginWord)
		backreached.add(endWord)
		wordList.discard(beginWord)
		wordList.discard(endWord)
		depth = 1
		while not reached & backreached:
			toAdd = set()
			for word in reached:
				for (i, letter) in enumerate(word):
					for newletter in letters:
						if newletter != letter:
							newword = word[:i] + newletter + word[i + 1:]
							if newword in backreached:
								return depth + 1
							elif newword in wordList:
								toAdd.add(newword)
								wordList.remove(newword)
			if len(toAdd) == 0:
				return 0
			else:
				depth += 1
				reached = toAdd
				if len(reached) > len(backreached):
					(reached, backreached) = (backreached, reached)
		return depth


class Solution:

	def ladderLength(self, beginWord, endWord, wordList):
		wordList = set(wordList)
		if endWord not in wordList:
			return 0
		beginSet = set()
		endSet = set()
		visited = set()
		res = 1
		beginSet.add(beginWord)
		endSet.add(endWord)
		while len(beginSet) != 0 and len(endSet) != 0:
			if len(beginSet) > len(endSet):
				(beginSet, endSet) = (endSet, beginSet)
			tmp = set()
			for word in beginSet:
				chs = list(word)
				for i in range(len(chs)):
					for ch in range(ord('a'), ord('z') + 1):
						old = chs[i]
						chs[i] = chr(ch)
						target = ''.join(chs)
						if target in endSet:
							return res + 1
						if target not in visited and target in wordList:
							tmp.add(target)
							visited.add(target)
						chs[i] = old
			beginSet = tmp
			res += 1
		return 0