Shuffle (A[1..m], B[1..n], C[1..m+n]):
Shuf[0, 0] ← True
for j ← 1 to n
Shuf[0, j] ← Shuf[0, j − 1] ∧ (B[j] = C[j])
for i ← 1 to n
Shuf[i, 0] ← Shuf[i − 1, 0] ∧ (A[i] = B[i])
for j ← 1 to n
Shuf[i, j] ← False
if A[i] = C[i + j]
Shuf[i, j] ← Shuf[i, j] ∨ Shuf[i − 1, j]
if B[i] = C[i + j]
Shuf[i, j] ← Shuf[i, j] ∨ Shuf[i, j − 1]
return Shuf[m, n]
The algorithm runs in O(mn) time.