Actually, this is a variant of the classic Longest Common Subsequence problem. You can solve this basic dynamic programming by first finding the LCS, and then adding '-' to unify the two given string. Recursive algorithm follows a top-down approach but often leads to low efficiency, while the non-recursive version could solve the basic LCS problem bottom up and helps you understand the core part of dynamic programming.
def lcs(a, b):
m, n = len(a), len(b)
stable = [[[0,'-'] for j in xrange(n+1)] for i in xrange(m+1)]
for i in xrange(1, m+1):
for j in xrange(1, n+1):
lu, l, u = stable[i-1][j-1][0], stable[i][j-1][0], stable[i-1][j][0]
stable[i][j][0] = (lu + 1) if a[i-1] == b[j-1] else max(l,u)
if a[i-1] == b[j-1]:
stable[i][j][1] = '`'
elif l > u:
stable[i][j][1] ='<'
else:
stable[i][j][1] ='^'
i, j = m, n
similara, similarb = list(a), list(b)
while i > 0 and j > 0:
if stable[i][j][1] == '`':
lcstring = a[i-1] + lcstring
i -= 1
j -= 1
elif stable[i][j][1] == '<':
similara.insert(i, '-')
j -= 1
elif stable[i][j][1] == '^':
similarb.insert(j, '-')
i -= 1
while len(similara) < len(similarb):
similara.insert(0,'-')
while len(similarb) < len(similara):
similarb.insert(0,'-')
return stable[len(a)][len(b)][0], lcstring,''.join(similara), ''.join(similarb)
you can get the length of LCS, the LCS itself, the 2 converted strings by the above function
>>>print lcs('sdfsdf', 'dffd')
3, 'dfd', sd-fsdf, -dff-d-