| Bytes | Lang | Time | Link |
|---|---|---|---|
| 272 | Python3 | 250824T162225Z | Ajax1234 |
| 216 | Python | 220729T022943Z | 97.100.9 |
| 097 | R | 220727T185242Z | pajonk |
| 015 | Vyxal | 220729T010203Z | naffetS |
| 017 | 05AB1E | 220727T131004Z | Kevin Cr |
| 163 | JavaScript ES6 | 220727T153000Z | Arnauld |
Python3, 272 bytes
l=lambda a,b:max(T)if 0 in(T:=[len(a),len(b)])else(l(a[1:],b[1:])if a[0]==b[0]else 1+min(l(a[1:],b),l(a,b[1:]),l(a[1:],b[1:])))
def f(a,b):
j=[]
for i in range(len(a)):
for I in range(i+1,len(a)):A=[*a];T=A[i];A[i]=A[I];A[I]=T;j+=[A]
return min(j,key=lambda x:l(x,b))
Python, 216 bytes
lambda a,b:(n:=len(a))and min(w(c:=[*a],k%n,k//n)or d(c,[*b])for k in range(n*n)if k%n<k//n) d=lambda s,t:-~min(d(S:=s[1:],T:=t[1:])-(s[0]==t[0]),d(S,t),d(s,T))if s>""<t else len(s+t) def w(a,i,j):a[i],a[j]=a[j],a[i]
A very bloated answer which just iterates through all pairs of swaps. The Levenshtein distance is adapted from an answer to this question, but slightly shortened.
R, 130 97 bytes
\(A,B,x=combn(seq(a<-utf8ToInt(A)),2,\(i)intToUtf8(`[<-`(a,i,a[rev(i)]))))x[order(adist(x,B))[1]]
Another brute-force solution.
Vyxal, 15 bytes
ė2ḋƛ∩÷Ṙ¹∇Ȧ;‡⁰꘍∵
ė2ḋƛ∩÷ could be ẏ2ḋƛ¹İ for the same byte count.
ė2ḋƛ∩÷Ṙ¹∇Ȧ;‡⁰꘍∵
ė # Enumerate, make a list [index, x] for each x in the first input.
2ḋ # Combinations without replacement of length 2; pairs.
ƛ # For each:
∩ # Transpose
÷ # Dump; push both items onto the stack.
Ṙ # Reverse the top pair (the pair of two characters). Call this Y, and the other pair X.
¹ # Push the first input
∇ # Push it under the top two values of the stack, aka c,a,b.
Ȧ # Assign; replace each of the indices in X with the corresponding value in Y.
; # Close map.
‡ ∵ # Minimum by:
⁰꘍ # Levenshtein distance with the second input.
05AB1E, 17 bytes
\ā<.ÆεèyRǝ}Σ¹.L}н
Inputs in the order \$B,A\$.
Try it online or verify all test cases.
ā<.Æεèy could alternatively be .ā.Æεø` for the same byte-count:
Try it online or verify all test cases.
Explanation:
\ # Discard the first (implicit) input `B`
ā # Push a list in the range [1, length of second (implicit) input `A`]
< # Decrease it to range [0, length_A)
.Æ # Get all pairs without duplicates
ε # Map over each pair:
è # Index them into the second (implicit) input `A`
yR # Push the pair reversed
ǝ # Insert them into the second (implicit) input `A`
}Σ # After the map: sort the strings by:
.L # The Levenshtein distance with
¹ # the first input `B`
}н # After the sort: pop and leave the first string
# (which is output implicitly as result)
.ā # Enumerate the second (implicit) input,
# pairing each character with its index
.Æ # Get all pairs without duplicates
ε # Map over each pair:
ø # Zip/transpose; swapping rows/columns
` # Pop and push the pair of characters and pair of indices separated to
# the stack
R # Reverse the indices-pair
JavaScript (ES6), 163 bytes
Expects (a)(b), where a and b are arrays of characters. Returns another array of characters.
This really just tries all possibilities and computes the Levenshtein distance for each of them. It seems very likely that there's a smarter/shorter way.
a=>M=b=>a.map((x,i)=>a.map((y,j,[...s])=>j<=i|(s[i]=y,s[j]=x,n=b.length,g=m=>v=m*n?1+Math.min(g(m,--n),g(--m)-(s[m]==b[n++]),g(m)):m+n)(s.length)>M||(M=v,o=s)))&&o