Here’s how to backspace-and-retype from one string to another:
- Start from the first string.
- Remove characters at the end until the result is a prefix of the second string. (This may take 0 steps.)
- Add characters at the end until the result equals the second string. (This may take 0 steps, too.)
For example, the path from
fooxyz looks like:
fooabc fooab fooa foo foox fooxy fooxyz
Given a list of words, write a program that backspace-and-retypes its way from the empty string, to all of the words in the list in succession, back to the empty string. Output all intermediate strings.
For example, given the input list
["abc", "abd", "aefg", "h"], the output should be:
a ab abc ab abd ab a ae aef aefg aef ae a h
You may return or print a list of strings, or a single string with some delimiter of choice. You may optionally include the initial and final empty strings. The input is guaranteed to contain at least one word, and each word is guaranteed to contain only lowercase ASCII letters (
z). Edit: consecutive strings in the input are guaranteed not to equal each other.
This is code-golf; shortest code in bytes wins.
A reference implementation in Python 3: Try it online!