Given two lowercase strings source and target, determine whether target can be obtained from source using at most one of the following operations, and output one valid operation if possible.
Allowed operations:
-
add c
: Insert one character
c
at any position in
source
.
-
delete c
: Delete one occurrence of character
c
from
source
.
-
replace x y
: Replace one occurrence of character
x
in
source
with character
y
.
-
move c
: Remove one occurrence of character
c
from its current position and insert it at another position in the string.
If source is already equal to target, output no_change. If no single operation can transform source into target, output impossible. If multiple valid operations exist, output any one valid operation.
Examples:
source = "baren", target = "bbren"
output: replace a b
source = "banan", target = "banana"
output: add a
source = "abcde", target = "acdeb"
output: move b
Expected follow-up: design an efficient solution that handles long strings, ideally in linear time.