Each step of the Levenshtein distance

In this challenge you will write a program that takes two newline-separated strings, s1 (the first line) and s2 (the second line), as input (STDIN or closest). You can assume that the length of s1 will always be smaller than 30 and bigger than the length of s2. The program should then output each step in the levenshtein distance from s1 to s2.

To clarify what each step in the levenshtein distance means, the program will print n strings, where n is the levenshtein distance between s1 and s2, and the levenshtein distance between two adjacent strings will always be one. The order doesn't matter. The output should be newline-separated and not include s1, only the in-betweens and s2. The program should also run in under one minute on a modern computer.

Examples:

Input:

Programming
Codegolf


Output:

rogramming
Cogramming
Coramming
Coamming
Codmming
Codeming
Codeging
Codegong
Codegolg
Codegolf


Input:

Questions


Output:

uestions
Aestions
Anstions
Ansions
Answons
Answens


Input:

Offline
Online


Output:

Ofline
Online


Input:

Saturday
Sunday


Output:

Sturday
Surday
Sunday


Here is a link to a python script that prints out the distance and the steps.

This is code-golf so keep you code short; shortest code wins!

• For my edit, I rather presumed that the input would be of the form s1(newline)s2, however, having looked over the question again, I am wondering if instead you intended for the program to select s1 and s2 based on the length of 2 inputed strings, coming in either order, would you mind clarifying this point? That is, do we assume the input is s1 followed by s2, or do we select s1 and s2 based on the length of the two inputs? Jun 7, 2015 at 17:25
• Does an answer have to run in a reasonable amount of time?
– KSab
Jun 7, 2015 at 18:30
• Camper - Ampere, distance 2, the python script runs forever ... Jun 9, 2015 at 13:47
• How strict is "take input from STDIN or closest"? Can I write a function that takes the input via function argument? The currently accepted answer does so.
– nimi
Mar 5, 2016 at 23:57

Haskell, 201 194 bytes

l=length
g[]n u=map(\_->"")n
g(b:c)[]u=(u++c):g c[]u
g(b:c)n@(o:p)u|b==o=g c p(u++[o])|1<2=((u++o:c):g c p(u++[o]))!((u++c):g c n u)
a!b|l a<l b=a|1<2=b
p[a,n]=g a n""
f=interact\$unlines.p.lines


Longer than expected. Maybe I can golf it down a little bit ...

Usage example:

*Main> f                     -- call via f
Questions                    -- User input
Answers                      -- no newline after second line!
uestions                     -- Output starts here
Aestions
Anstions
Ansions
Answons
Answens


It's a brute force that decides between changing and deleting if the initial characters differ.

• How long does it take to run? Jun 9, 2015 at 5:12
• How can I test (maybe ideone)? Jun 9, 2015 at 13:49
• @Loovjo: shorter strings like your examples are calculated instantly, worst case is about 1:30min. I've interpreted the "should" in "should run in under one minute" not as a strict limit (should vs. must). If this is a must, I can add a "performance pack" for about 20 bytes.
– nimi
Jun 9, 2015 at 15:09
• @edc65: yes, ideone, but it expects the function to be executed to be called "main". Try: ideone.com/CUgU8W
– nimi
Jun 9, 2015 at 15:21

APL (Dyalog 18.0), 66 bytes

{⍺<Ö≢⍵:⍺∇⎕←0~⍨0@(⊃(⍸~p)~⍳⊃⌽⍸p←⍵=⍺↑⍨≢⍵)⊢⍵⋄j←⊃⍸⍺≠⍵⋄⍺≢⍵:⍺∇⎕←⍺[j]@j⊢⍵}


Try it online!

Works correctly now.

A recursive function submission, done with a lot of input from Adám and Bubbler.

Input is taken as <s2> f <s1>.

Explanation

⍺<⍥≢⍵: if the strings have different lengths:

0@(...)⊢⍵ put a zero at the following index:

⊃ first element of:

(⍸~p) characters which do not match

~ without

⍳⊃⌽⍸p←⍵=⍺↑⍨≢⍵ all indices before last character match

0~⍨ remove the zero(thereby removing the element)

⍺∇⎕← display the result and recurse

⋄⍺≢⍵: Otherwise, till the strings match:

i←⊃⍸⍺≠⍵ find first non-matching element

⍺[i]@i⊢⍵ and replace the element at that index

⍺∇⎕← display the result, and recurse