Aligning one letter with a word #
The singleton base case of Hirschberg reconstruction scans the target from right to left. At each suffix it carries the cost and script both for inserting that suffix and for aligning the source letter with it. Each extension compares replacing the source letter now against inserting the new target letter before the best alignment already found. The empty target deletes the source letter, so the scan also considers deleting it and inserting the entire target.
An optimal alignment from [a] to y, computed by one scan of the target. If replacing
and inserting the next target letter achieve equal costs, prefer the replacement.
The singleton scan consumes exactly the source letter, produces the target word, and attains the Wagner–Fischer distance for arbitrary natural-valued letter costs.