@Jason1016 Similar to yours, but instead of adding to an visited set, I remove from a not_visited set and use this not_visited set in the possible_mutation function.

from collections import deque class Solution(object): def minMutation(self, start, end, bank): """ :type start: str :type end: str :type bank: List[str] :rtype: int """ not_visited = set(bank) def mutations_generator(gene): for mutation in list(not_visited): diff = 0 for i in range(0, len(gene)): if diff > 1: continue elif gene[i] != mutation[i]: diff += 1 if diff == 1: not_visited.remove(mutation) yield mutation open_nodes = deque() open_nodes.append((start, 0)) while len(open_nodes) > 0: gene_tuple = open_nodes.pop() if gene_tuple[0] == end: return gene_tuple[1] next_mutation_count = gene_tuple[1] + 1 for mutation in mutations_generator(gene_tuple[0]): open_nodes.append((mutation, next_mutation_count)) return -1