Conjecture about reversal operations on strings (with duplicates)
Here is a check in Python that your proposed counterexample is indeed a counterexample. I took the subflip
function from @AleksejsFomins's answer.
import itertools
def subflip(s, i, j):
return s[:i] + s[i:j][::-1] + s[j:]
s = "22131441"
t = "41211342"
flipped_strings = set([s])
num_flips = 0
while True:
if t in flipped_strings:
print("Minimal number of flips:", num_flips)
break
new_flipped_strings = set()
for i, j in itertools.combinations(range(len(s) + 1), 2):
new_flipped_strings.update(subflip(f, i, j) for f in flipped_strings)
flipped_strings = new_flipped_strings
num_flips += 1
The output is (near instantaneously):
Minimal number of flips: 4
And indeed, if you prepend the 4
on both strings this script confirms that three flips suffice to change one string into the other.
I have run a numerical simulation on your proposed counter-example, and the minimal number of flips for the string without prefix is 4, which is more than 3
CODE (Python3)
def subflip(s, i, j):
return s[:i] + s[i:j][::-1] + s[j:]
s1 = "22131441"
s2 = "41211342"
N = len(s1)
def test(s1, s2, pref, depth):
for i in range(N-1):
for j in range(i+2, N+1):
s1_a = subflip(s1, i, j)
pref_a = pref+[(i, j)]
if s1_a == s2:
print(s1_a, s2)
print("Win", pref_a)
return True
if depth - 1 > 0:
if test(s1_a, s2, pref_a, depth-1):
return True
return False
test(s1, s2, [], 4)
Answer:
Win [(0, 4), (0, 6), (3, 8), (4, 7)]
Edit: This is a depth-first search, so I ran the code several times for increasing value of depth until I got an answer
Note: I do not believe I deserve the bounty, because you proposed the counter-example yourself, I just checked it