Given two distinct words startWord and targetWord, and a list denoting wordList of unique words of equal lengths.  Find all shortest transformation sequence(s) from startWord to targetWord. You can return them in any order possible.
Keep the following conditions in mind:
	A word can only consist of lowercase characters.
	Only one letter can be changed in each transformation.
	Each transformed word must exist in the wordList including the targetWord.
	startWord may or may not be part of the wordList. 
	Return an empty list if there is no such transformation sequence.
The first part of this problem can be found here.
Example 1:
Input:
startWord = "der", targetWord = "dfs",
wordList = {"des","der","dfr","dgt","dfs"}
Output:
der dfr dfs
der des dfs
Explanation:
The length of the smallest transformation is 3.
And the following are the only two ways to get
to targetWord:-
"der" -> "des" -> "dfs".
"der" -> "dfr" -> "dfs".
Example 2:
Input:
startWord = "gedk", targetWord = "geek", 
wordList = {"geek", "gefk"}
Output:
"gedk" -> "geek"
Your Task:
You don't need to read or print anything, Your task is to complete the function findSequences() which takes startWord, targetWord and wordList as input parameter and returns a list of list of strings of the shortest transformation sequence from startWord to targetWord.
Note: You don't have to return -1 in case of no possible sequence. Just return the Empty List.
Expected Time Compelxity: O(N*(logN * M * 26))
Expected Auxiliary Space: O(N * M) where N = length of wordList and M = |wordList_{i}|
Constraints:
1 ≤ N ≤ 100
1 ≤ M ≤ 10

Source: geeksforgeeks | Difficulty: MEDIUM_HARD
Tags: Data structures

Graph algorithms

Graph traversal

Starter code: #User function Template for python3



class Solution:

    def findSequences(self, startWord, targetWord, wordList):

        #Code here

Solutions:
from collections import deque

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		st = set(wordList)
		queue = deque()
		queue.append([startWord])
		ans = []
		usedonLevel = [startWord]
		level = 0
		while queue:
			vec = queue.popleft()
			if len(vec) > level:
				level += 1
				for ele in usedonLevel:
					st.discard(ele)
			word = vec[-1]
			if word == targetWord:
				if not ans:
					ans.append(vec)
				elif len(ans[0]) == len(vec):
					ans.append(vec)
			for i in range(0, len(word)):
				original = word[i]
				for ch in 'abcdefghijklmnopqrstuvwxyz':
					word = word[:i] + ch + word[i + 1:]
					if word in st:
						vec.append(word)
						queue.append(vec.copy())
						usedonLevel.append(word)
						vec.pop()
					word = word[:i] + original + word[i + 1:]
		return ans


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		wordList = set(wordList)
		queue = []
		queue.append((startWord,))
		usedOnLevel = []
		usedOnLevel.append(startWord)
		level = 0
		ans = []
		while len(queue) > 0:
			vec = queue.pop(0)
			if len(vec) > level:
				level += 1
				for v in usedOnLevel:
					if v in wordList:
						wordList.remove(v)
				usedOnLevel.clear()
			word = vec[-1]
			if word == targetWord:
				if len(ans) == 0:
					ans.append(list(vec))
				elif len(ans[0]) == len(vec):
					ans.append(list(vec))
			for i in range(0, len(word)):
				for ch in range(97, 123):
					new_word = str(word[:i] + chr(ch) + word[i + 1:])
					if new_word in wordList:
						new_vec = vec + (new_word,)
						queue.append(new_vec)
						usedOnLevel.append(new_word)
						new_vec = vec
		return ans


from typing import List

class Solution:

	def findSequences(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
		st = set(wordList)
		q = []
		q.append([beginWord])
		used = []
		used.append(beginWord)
		level = 0
		ans = []
		while q:
			vec = q.pop(0)
			if len(vec) > level:
				level += 1
				for it in used:
					st.discard(it)
				used.clear()
			word = vec[-1]
			if word == endWord:
				if len(ans) == 0:
					ans.append(vec)
				elif len(ans[0]) == len(vec):
					ans.append(vec)
			for i in range(len(word)):
				original = word[i]
				for ch in range(ord('a'), ord('z') + 1):
					word = word[:i] + chr(ch) + word[i + 1:]
					if word in st:
						vec.append(word)
						q.append(vec[:])
						used.append(word)
						vec.pop()
				word = word[:i] + original + word[i + 1:]
		return ans


from collections import defaultdict as dq

class Solution:

	def findSequences(self, start, end, words):
		d = dq(int)
		for i in words:
			if i != start:
				d[i] = 1
		q = [[start]]
		ans = []
		con = 0
		ptr = 0
		br = 1
		mn = 100000000
		while ptr < len(q):
			d[q[ptr][-1]] = 0
			for i in range(len(q[ptr][-1])):
				for j in range(26):
					check = list(q[ptr][-1])
					check[i] = chr(65 + j).lower()
					check = ''.join(check)
					if d[check] == 1:
						q.append([*q[ptr], check])
						if check == end:
							if con == 0:
								ans.append(q[-1])
								mn = min(len(q[-1]), mn)
				if br == 0:
					break
			if br == 0:
				break
			ptr += 1
		an = []
		for i in ans:
			if len(i) == mn:
				an.append(i)
		return an


class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		from collections import deque
		st = set(wordList)
		q = deque([[beginWord]])
		usedOnLevel = [beginWord]
		level = 0
		ans = []
		while q:
			vec = q.popleft()
			if len(vec) > level:
				level += 1
				for word in usedOnLevel:
					st.discard(word)
			word = vec[-1]
			if word == endWord:
				if len(ans) == 0:
					ans.append(vec)
				elif len(ans[0]) == len(vec):
					ans.append(vec)
			for i in range(len(word)):
				original = word[i]
				for c in 'abcdefghijklmnopqrstuvwxyz':
					word = word[:i] + c + word[i + 1:]
					if word in st:
						vec.append(word)
						q.append(vec.copy())
						usedOnLevel.append(word)
						vec.pop()
					word = word[:i] + original + word[i + 1:]
		return ans


from collections import defaultdict

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		d = defaultdict(list)
		q = []
		q.append((startWord, [startWord]))
		s = set([startWord])
		wordList.append(startWord)
		wordList = set(wordList)
		res = []
		for word in wordList:
			for j in range(len(word)):
				pat = word[:j] + '*' + word[j + 1:]
				d[pat].append(word)
		while q:
			for i in range(len(q)):
				(word, seq) = q.pop(0)
				if word == targetWord:
					res.append(seq)
				for j in range(len(word)):
					pat = word[:j] + '*' + word[j + 1:]
					for k in d[pat]:
						if k in wordList:
							s.add(k)
							q.append((k, seq + [k]))
			wordList -= s
		return res


from collections import deque

class Solution:

	def findSequences(self, s, t, w):
		global ans, setx
		ans = []
		l = len(s)
		setx = set(w)
		if s in setx:
			setx.remove(s)
		if t not in setx:
			return []
		q = [s, '$']
		c = 1
		f = 0
		while q:
			ele = q.pop(0)
			if ele == '$':
				if not q:
					return []
				c += 1
				q.append('$')
				continue
			for i in range(l):
				for j in range(97, 123):
					a = chr(j)
					st = ele[:i] + a + ele[i + 1:]
					if st in setx:
						if st == t:
							c += 1
							f = 1
						q.append(st)
						setx.remove(st)
			if f == 1:
				break
		setx = set(w)
		if s in setx:
			setx.remove(s)
		dfs(s, t, c, [s], 1, l, setx)
		return ans

def dfs(s, t, c, path, k, l, setx):
	global ans
	if k == c:
		if path[-1] == t:
			ans.append(path)
			setx.add(t)
		return
	for i in range(l):
		for j in range(97, 123):
			a = chr(j)
			st = s[:i] + a + s[i + 1:]
			if st in setx:
				setx.remove(st)
				dfs(st, t, c, path + [st], k + 1, l, setx)
				setx.add(st)


class Solution:

	def findSequences(self, beginWord, endWord, wordList):

		def dfs(word, a):
			if word == beginWord:
				ans.append(a[::-1])
				return
			n = d[word] - 1
			for i in range(l):
				for j in range(26):
					new = word[:i] + chr(97 + j) + word[i + 1:]
					if new in d and d[new] == n:
						a.append(new)
						dfs(new, a)
						a.pop()
			return ans
		wordList = set(wordList)
		wordList.discard(beginWord)
		if endWord not in wordList:
			return []
		q = deque([beginWord])
		ans = []
		d = dict()
		d[beginWord] = 1
		level = 1
		t = set()
		l = len(beginWord)
		while q:
			word = q.popleft()
			if word == endWord:
				break
			for i in range(l):
				for j in range(26):
					new = word[:i] + chr(97 + j) + word[i + 1:]
					if new in wordList:
						d[new] = d[word] + 1
						q.append(new)
						wordList.remove(new)
		if endWord in d:
			dfs(endWord, [endWord])
		return ans


class Solution:

	def findSequences(self, start, end, wordList):
		from collections import deque
		wordlist = set(wordList)
		q = deque()
		q.append([start, [start]])
		alphabets = 'abcdefghijklmnopqrstuvwxyz'
		res = []
		while q:
			temp = []
			for _ in range(len(q)):
				(word, lst) = q.popleft()
				if word == end:
					res.append(lst)
					continue
				for ind in range(len(word)):
					for letter in alphabets:
						new = word[:ind] + letter + word[ind + 1:]
						if new in wordlist:
							temp.append(new)
							q.append([new, lst + [new]])
			for newword in temp:
				wordlist.discard(newword)
		return res


from collections import deque

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		q = deque([[startWord]])
		wordlist = set(wordList)
		temp = set()
		temp.add(startWord)
		alphabets = 'abcdefghijklmnopqrstuvwxyz'
		res = []
		while q:
			temp = set()
			for _ in range(len(q)):
				lst = q.popleft()
				word = lst[-1]
				if word == targetWord:
					res.append(lst)
					continue
				for ind in range(len(word)):
					for letter in alphabets:
						newWord = word[:ind] + letter + word[ind + 1:]
						if newWord in wordlist:
							q.append(lst + [newWord])
							temp.add(newWord)
			for wd in temp:
				wordlist.discard(wd)
		return res


class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		wordList = set(wordList)
		if beginWord == endWord or endWord not in wordList:
			return []
		q = deque()
		q.append([beginWord])
		ans = []
		visited = set([beginWord])
		while q and (not ans):
			t = set()
			for _ in range(len(q)):
				a = q.popleft()
				word = a[-1]
				for i in range(len(beginWord)):
					for j in range(26):
						new = word[:i] + chr(97 + j) + word[i + 1:]
						if new == endWord:
							ans.append(a + [endWord])
						if new in wordList and new not in visited:
							t.add(new)
							q.append(a + [new])
			visited = visited.union(t)
		return ans


from collections import deque

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		avail = set(wordList)
		queue = deque([[startWord]])
		usedOnLevel = [startWord]
		ans = []
		while queue:
			n = len(queue)
			for used in usedOnLevel:
				avail.discard(used)
			for _ in range(n):
				word_list = queue.popleft()
				word = word_list[-1]
				if word == targetWord:
					ans.append(word_list)
				for i in range(len(word)):
					for ch in range(ord('a'), ord('z') + 1):
						temp = word[:i] + chr(ch) + word[i + 1:]
						if temp in avail:
							usedOnLevel.append(temp)
							word_list.append(temp)
							queue.append(word_list[:])
							word_list.remove(temp)
		return ans


from collections import deque
import string

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		if targetWord not in wordList:
			return []
		ans = []
		queue = deque([[startWord]])
		visited = set()
		word = set(wordList)
		while queue:
			layer = set()
			for _ in range(len(queue)):
				cur = queue.popleft()
				last = cur[-1]
				if last == targetWord:
					ans.append(cur)
				for i in range(len(startWord)):
					for c in string.ascii_lowercase:
						next = last[:i] + c + last[i + 1:]
						if next in word and next not in visited:
							queue.append(cur + [next])
							layer.add(next)
			visited.update(layer)
		return ans


from collections import deque

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		st = set(wordList)
		q = deque()
		q.append([startWord])
		used = []
		used.append(startWord)
		level = 0
		ans = []
		while q:
			vec = q.popleft()
			if len(vec) > level:
				level += 1
				for it in used:
					if it in st:
						st.remove(it)
			word = vec[-1]
			if word == targetWord:
				if len(ans) == 0:
					ans.append(vec)
				elif len(ans[0]) == len(vec):
					ans.append(vec)
			for i in range(len(word)):
				for ch in 'abcdefghijklmnopqrstuvwxyz':
					ele = word[:i] + ch + word[i + 1:]
					if ele in st:
						vec.append(ele)
						q.append(vec.copy())
						used.append(ele)
						vec.remove(ele)
		return ans


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		q = [[startWord]]
		usedword = [startWord]
		s = set(wordList)
		ans = []
		level = 0
		while q:
			a = q.pop(0)
			if len(a) > level:
				level += 1
				for i in usedword:
					if i in s:
						s.remove(i)
			if a[-1] == targetWord:
				if len(ans) == 0:
					ans.append(a)
				if len(a) == len(ans[0]) and a not in ans:
					ans.append(a)
			word = a[-1]
			word = list(word)
			for j in range(len(word)):
				org = word[j]
				for k in alpha:
					word[j] = k
					sord = ''.join(word)
					if sord in s:
						q.append(a + [sord])
						usedword.append(sord)
				word[j] = org
		return ans


from collections import deque
from copy import deepcopy

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		q = deque()
		unSet = set(wordList)
		usedOnLevel = []
		level = 0
		ans = []
		q.append([startWord])
		usedOnLevel.append(startWord)
		if startWord in unSet:
			unSet.remove(startWord)
		while q:
			path = q.popleft()
			if len(path) > level:
				level += 1
				for ele in usedOnLevel:
					if ele in unSet:
						unSet.remove(ele)
				usedOnLevel = []
			last = path[-1]
			if last == targetWord:
				if len(ans) == 0:
					ans.append(path)
				elif len(ans[0]) == len(path):
					ans.append(path)
			for i in range(len(last)):
				old = last[i]
				for j in range(ord('a'), ord('z') + 1):
					last = last[:i] + chr(j) + last[i + 1:]
					if last in unSet:
						path.append(last)
						q.append(deepcopy(path))
						usedOnLevel.append(last)
						path.pop()
				last = last[:i] + old + last[i + 1:]
		return ans


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		import collections
		beginWord = startWord
		endWord = targetWord
		graph = collections.defaultdict(list)
		if endWord not in wordList:
			return []
		for word in wordList:
			for j in range(len(word)):
				pattern = word[:j] + '*' + word[j + 1:]
				graph[pattern].append(word)
		q = [[beginWord]]
		visited = {beginWord}
		res = []
		while q:
			visited2 = set()
			lenq = len(q)
			for i in range(lenq):
				word = q.pop(0)
				if word[-1] == endWord:
					res.append(word.copy())
					continue
				for j in range(len(word[-1])):
					pattern = word[-1][:j] + '*' + word[-1][j + 1:]
					for node in graph[pattern]:
						if node not in visited:
							q.append(word + [node])
							visited2.add(node)
			visited.update(visited2)
			if len(res) > 0:
				break
		return res


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		words = set(wordList)
		m = len(startWord)
		qu = deque([(startWord, [startWord])])
		ans = []
		while qu:
			l = len(qu)
			used = set()
			f = False
			for _ in range(l):
				(curr, pathsofar) = qu.popleft()
				for i in range(m):
					for j in range(97, 123):
						word = curr[:i] + chr(j) + curr[i + 1:]
						if word in words:
							if word == targetWord:
								f = True
								ans.append(pathsofar + [word])
							else:
								qu.append([word, pathsofar + [word]])
								used.add(word)
			while used:
				words.remove(used.pop())
			if f:
				break
		return ans


from collections import deque

class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		wordSet = set(wordList)
		if beginWord == endWord or endWord not in wordSet:
			return []
		queue = deque()
		queue.append([beginWord])
		result = []
		distance = float('-inf')
		while queue:
			for _ in range(len(queue)):
				path = queue.popleft()
				lastWord = path[-1]
				wordSet.discard(lastWord)
				if distance > len(path):
					return result
				if lastWord == endWord:
					if distance == float('-inf') or distance == len(path):
						result.append(path)
						distance = len(path)
					continue
				for ch in 'abcdefghijklmnopqrstuvwxyz':
					for i in range(len(lastWord)):
						newWord = lastWord[:i] + ch + lastWord[i + 1:]
						if newWord not in wordSet:
							continue
						queue.append(path + [newWord])
		return result


from collections import deque

class Solution:

	def findSequences(self, startword, targetword, wordlist):
		q = deque()
		q.append([startword])
		s = set(wordlist)
		s.discard(startword)
		ans = []
		level = 0
		used = []
		while len(q) > 0:
			st = q.popleft()
			if len(st) > level:
				level += 1
				for i in used:
					s.discard(i)
				used = []
			word = st[-1]
			if word == targetword:
				ans.append(st)
			for i in range(len(word)):
				ch = word[i]
				for j in range(97, 123):
					word = word[:i] + chr(j) + word[i + 1:]
					if word in s:
						st.append(word)
						q.append(st.copy())
						st.pop()
						used.append(word)
				word = word[:i] + ch + word[i + 1:]
		return ans


from collections import deque

class Solution:

	def findSequences(self, startword, targetword, wordlist):
		s = set(wordlist)
		q = deque()
		q.append([startword])
		ans = []
		level = 0
		used = []
		used.append(startword)
		while q:
			st = q.popleft()
			if len(st) > level:
				level += 1
				for i in used:
					s.discard(i)
				used = []
			word = st[-1]
			if word == targetword:
				if not ans:
					ans.append(st)
				elif len(ans[0]) == len(st):
					ans.append(st)
			for i in range(len(word)):
				ch = word[i]
				for c in 'abcdefghijklmnopqrstuvwxyz':
					word = word[:i] + c + word[i + 1:]
					if word in s:
						st.append(word)
						q.append(st.copy())
						st.pop()
						used.append(word)
				word = word[:i] + ch + word[i + 1:]
		return ans


import copy

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		s = set(wordList)
		from collections import deque
		queue = deque()
		queue.append([startWord])
		if startWord in s:
			s.remove(startWord)
		ans = []
		while queue:
			se = set()
			for i in range(len(queue)):
				wordlis = copy.copy(queue.popleft())
				word = wordlis[-1]
				if word == targetWord:
					ans.append(wordlis)
					continue
				for i in range(len(startWord)):
					for j in range(26):
						newword = word[:i] + chr(97 + j) + word[i + 1:]
						if newword in s:
							wordlis.append(newword)
							queue.append(copy.copy(wordlis))
							se.add(newword)
							wordlis.remove(newword)
			for i in se:
				s.remove(i)
		return ans


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		(sw, tw) = (startWord, targetWord)
		wl = {}
		for i in wordList:
			wl[i] = wl.get(i, 0) + 1
		ans = []
		vis = {}
		l1 = [[sw]]
		n = len(sw)
		l2 = []
		while len(l1) != 0:
			tvis = {}
			flag = False
			for tl in l1:
				word = tl[-1]
				for i in range(n):
					for j in range(97, 122 + 1):
						w2 = word[0:i] + chr(j) + word[i + 1:]
						if w2 in wl and w2 not in vis:
							ttl = tl[:]
							ttl.append(w2)
							l2.append(ttl)
							tvis[w2] = tvis.get(w2, 0) + 1
						if w2 == tw:
							flag = True
			for i in tvis:
				vis[i] = vis.get(i, 0) + 1
			if flag:
				for i in l2:
					if i[-1] == tw:
						ans.append(i)
				break
			l1 = l2[:]
			l2 = []
		if flag == False:
			return []
		return ans


from collections import defaultdict

class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		visited = dict()
		checkSet = set(wordList)
		transitions = defaultdict(set)
		q = [[startWord, 1]]
		ans = []
		levels = 1
		while q:
			a = q.pop(0)
			visited[a[0]] = 1
			transitions[a[1]].add(a[0])
			if a[0] == targetWord:
				levels = a[1]
				break
			for i in range(len(a[0])):
				for j in range(26):
					temp = a[0][0:i] + chr(j + 97) + a[0][i + 1:]
					if temp not in visited and temp in checkSet:
						q.append([temp, a[1] + 1])

		def dfs(word, levels, curr):
			curr.append(word)
			if levels == 1:
				if word in transitions[levels]:
					nonlocal ans
					temp = curr.copy()
					temp.reverse()
					ans.append(temp)
				return
			for i in range(len(word)):
				for j in range(26):
					temp = word[0:i] + chr(j + 97) + word[i + 1:]
					if temp in transitions[levels - 1]:
						dfs(temp, levels - 1, curr)
						curr.pop()
		dfs(targetWord, levels, [])
		return ans


from collections import deque

class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		s = set(wordList)
		queue = deque([])
		queue.append([beginWord])
		usedOnLevel = [beginWord]
		level = 0
		ans = []
		while queue:
			currPath = queue.popleft()
			if len(currPath) > level:
				level += 1
				for used in usedOnLevel:
					s.discard(used)
				usedOnLevel = []
			word = currPath[-1]
			if word == endWord:
				if ans == []:
					ans.append(currPath)
				elif len(ans[0]) == len(currPath):
					ans.append(currPath)
				continue
			word = list(word)
			for i in range(len(word)):
				original = word[i]
				for ch in range(97, 123):
					word[i] = chr(ch)
					newWord = ''.join(word)
					if newWord in s:
						temp = currPath.copy()
						temp.append(newWord)
						queue.append(temp)
						usedOnLevel.append(newWord)
				word[i] = original
		return ans


class Solution:

	def findSequences(self, startWord, targetWord, wordList):
		q = [[startWord]]
		remain = set(wordList)
		if startWord in remain:
			remain.remove(startWord)
		usedWords = set()
		level = 0
		res = []
		while q:
			present = q.pop(0)
			if level < len(present):
				level = len(present)
				for i in usedWords:
					remain.remove(i)
				usedWords = set()
			if present[-1] == targetWord:
				if len(res) == 0:
					res.append(present)
				elif len(present) == len(res[0]):
					res.append(present)
			for i in range(len(present[-1])):
				for j in range(25):
					check = present[-1][:i] + chr(97 + j) + present[-1][i + 1:]
					if check in remain:
						usedWords.add(check)
						q.append(present + [check])
		return res


from queue import Queue

class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		d = {}
		for word in wordList:
			l = len(word)
			for j in range(l):
				prev_s = word[:j]
				cur = '*'
				next_s = word[j + 1:]
				new_word = prev_s + cur + next_s
				if d.get(new_word, None) == None:
					d[new_word] = [word]
				else:
					d[new_word].append(word)
		words = {}
		for word in wordList:
			words[word] = True
		q1 = Queue()
		q1.put([beginWord])
		used_on_level = []
		used_on_level.append(beginWord)
		ans = []
		vis_d = {}
		level = 0
		while q1.qsize() != 0:
			vec = q1.get()
			if len(vec) > level:
				level = level + 1
				for w in used_on_level:
					if words.get(w, None) != None:
						words.pop(w)
				used_on_level = []
			word = vec[len(vec) - 1]
			if word == endWord:
				if len(ans) == 0:
					ans.append(vec)
				elif len(ans[0]) == len(vec):
					ans.append(vec)
			for i in range(len(word)):
				prev_s = word[:i]
				cur = '*'
				next_s = word[i + 1:]
				new_word = prev_s + cur + next_s
				if d.get(new_word, None) != None:
					neigh = d.get(new_word)
					for j in neigh:
						if words.get(j, None) != None:
							used_on_level.append(j)
							p_n = [gh for gh in vec]
							p_n.append(j)
							q1.put(p_n)
		return ans


class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		st = set(wordList)
		queue = []
		queue.append([beginWord])
		usedOnLevel = []
		usedOnLevel.append(beginWord)
		level = 0
		ans = []
		while queue:
			lst = queue.pop(0)
			if len(lst) > level:
				level += 1
				for i in usedOnLevel:
					try:
						st.remove(i)
					except KeyError:
						pass
			word = lst[-1]
			if word == endWord:
				if len(ans) == 0:
					ans.append(lst)
				elif len(ans[0]) == len(lst):
					ans.append(lst)
			for i in range(len(word)):
				og = word
				for j in range(26):
					word = word[:i] + chr(j + ord('a')) + word[i + 1:]
					if word in st:
						lst.append(word)
						queue.append(lst.copy())
						usedOnLevel.append(word)
						lst.pop()
				word = og
		return ans


import collections

class Solution:

	def findSequences(self, beginWord, endWord, wordList):
		(tree, words, n) = (collections.defaultdict(set), set(wordList), len(beginWord))
		if endWord not in wordList:
			return []
		(found, q, nq) = (False, {beginWord}, set())
		while q and (not found):
			words -= set(q)
			for x in q:
				for y in [x[:i] + c + x[i + 1:] for i in range(n) for c in 'qwertyuiopasdfghjklzxcvbnm']:
					if y in words:
						if y == endWord:
							found = True
						else:
							nq.add(y)
						tree[x].add(y)
			(q, nq) = (nq, set())
		return self.solve(beginWord, endWord, tree)

	def solve(self, x, endWord, tree):
		return [[x]] if x == endWord else [[x] + rest for y in tree[x] for rest in self.solve(y, endWord, tree)]