Word changer is a game where you are trying to turn one word into another via single-character edits, with each step being its own word. For this challenge, edits may be replacements, insertions, or deletions. For example, WINNER → LOSER can be done with this route (there may be others):
WINNER
DINNER
DINER
DINE
LINE
LONE
LOSE
LOSER
Phrased another way, you must be able to reach one word from the other going only through other words at a Levenshtein distance of 1 each time.
Coding
You will be given a word list and two words and you must output a valid route from one word to the other if a route exists or a distinct constant value or consistent behavior if no route exists.
- You may assume that the input words are both in the word list
- The word list can be taken in via any convenient flat format.
- Lists, sets, tries, space-separated strings, and line-separated files are all valid (for instance), but a pre-computed graph of Levenshtein adjacency is not.
- The output route should include both input words, but which starts and ends doesn't matter.
- If no route is found, you can output a specific constant, a falsy value, empty list, throw an exception, exit with a nonzero code, or any other behavior that happens in finite time.
- The route does not need to be optimal and there is no requirement of which route should be taken
- Computational complexity does not matter, however your program must be provably guaranteed to terminate in a finite amount of time. (even if it would run beyond the heat death of the universe)
- You may assume all words are entirely composed of letters in the same case
Example Test Cases
- CAT → DOG; [CAT, DOG, COG, COT, FROG, GROG, BOG]
- CAT, COT, COG, DOG
- BATH → SHOWER; [BATH, SHOWER, HATH, HAT, BAT, SAT, SAW, SOW, SHOW, HOW]
- No Route Found
- BREAK → FIX; [BREAK, FIX, BEAK, BREAD, READ, BEAD, RED, BED, BAD, BID, FAD, FAX]
- BREAK, BREAD, BEAD, BAD, FAD, FAX, FIX
- BUILD → DESTROY; [BUILD, DESTROY, BUILT, GUILT, GUILD, GILD, GILL, BILL, DILL, FILL, DESTRUCT, STRUCTURE, CONSTRUCT]
- No Route Found
- CARD → BOARD; [CARD, BOARD, BARD]
- CARD, BARD, BOARD
- DEMON → ANGEL; [DEMON, ANGEL]
- No Route Found
- LAST → PAST; [LAST, PAST, BLAST, CAST, BLACK, GHOST, POST, BOAST]
- LAST, PAST
- INSERT → DELETE; This word list
- INSERT, INVERT, INVENT, INBENT, UNBENT, UNBEND, UNBIND, UNKIND, UNKING, INKING, IRKING, DIRKING, DARKING, DARLING, ARLING, AILING, SIRING, SERING, SERINE, NERINE, NERITE, CERITE, CERATE, DERATE, DELATE, DELETE