importcollectionsclassSolution:# @param start, a string# @param end, a string# @param dict, a set of string# @return an integerdefladderLength(self,start,end,dict):# write your code here# queue: <word,level>queue=collections.deque([(start,1)])# if you dont add end into the dict, you will never find the final destinationdict.add(end)whilelen(queue)!=0:curr=queue.popleft()level=curr[1]cur=curr[0]ifcur==end:returnlevelelse:wordL=list(cur)foriin'abcdefghijklmnopqrstuvwxyz':forjinrange(len(wordL)):left_word=cur[:j]right_word=cur[j+1:]ifi!=cur[j]:# new_word is the word may exist in the dictnew_word=left_word+i+right_wordifnew_wordindict:queue.append((new_word,level+1))dict.remove(new_word)return0