Skip to content

Instantly share code, notes, and snippets.

@RohitK09
Created February 20, 2024 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RohitK09/9986d88cc0cee56076013cdc842726f7 to your computer and use it in GitHub Desktop.
Save RohitK09/9986d88cc0cee56076013cdc842726f7 to your computer and use it in GitHub Desktop.
from collections import deque
import string
def string_transformation(words, start, stop):
"""
Args:
words(list_str)
start(str)
stop(str)
Returns:
list_str
"""
# create set for dict
word_dict = set(words)
#create queue, visited, distance
q = deque()
q.append((list(start)))
n = 26
visited = set()
parent = {} # map of map to trace the path
res = []
while q:
curr_word = q.popleft()
curr_word_str = "".join(curr_word)
for i in range(len(start)):
old_char = curr_word[i]
word_str = ''.join(curr_word)
if word_str == stop:
res = []
while(word_str in parent):
word_str = parent[word_str]
res.append(word_str)
return res
for ch in list(string.ascii_lowercase):
curr_word[i]=ch
word_str = ''.join(curr_word)
if word_str not in visited or word_str == stop:
if word_str in word_dict or word_str == stop :
visited.add(word_str)
parent[word_str] = curr_word_str
q.append(list(curr_word.copy()))
res.append(str(word_str))
curr_word[i] = old_char
'''
#change the first char
put a to z in its place and if in dictionary, then
increment distance for that r and col
#
'''
return ["-1"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment