import itertools as i D=dict.fromkeys I=isinstance L=len P=i.product T=tuple def w(s,q=[]): if s: if q: if Z:=q[-1][0]:yield from w(s[1:],q[:-1]+[(Z,q[-1][1]+[s[0]])]) yield from w(s[1:],q+[(1>Z,[s[0]])]) else:yield from w(s[1:],q+[(0,[s[0]])]);yield from w(s[1:],q+[(1,[s[0]])]) else:yield T((a,T(set(b)))for a, b in q) def p(s,l=0): if L(s)==1:yield T(s);return n=min(s,key=L) if m:=[n[x:y+1]for x in range(L(n))for y in range(x,L(n))if all(n[x:y+1]in a for a in s)]: for q in m: a,b=[[*filter(None,x)]for x in zip(*[j.split(q,1)for j in s])];x,y=[p(a,1)]if a else[],[p(b,1)]if b else[];yield from P(*(x+[(q,)]+y)) if 1>l:yield(*[(u,)for u in D(a)],q,*[(u,)for u in D(b)]) if 1>l: for j in w(s): if~-all(x[0]for x in j):yield from P(*[p(b,1)if a else[b]for a,b in j]) else:s=[x for y in s for x in([[y],y][any(any(j in v for v in s if v!=y)for j in y)])];yield T([(u,)for u in sorted(set(s),key=s.index)]) def e(s):yield from map(''.join,P(*[[k]if I(k,str)else[*e(k),'']for k in s])) def t(s): for i in s: if I(i,str):yield i elif any(I(j,str)for j in i):yield T(t(i)) else:yield from t(i) def f(s):s=s.split(', ');return min([((v:=T(t(i))),sum(~-(j in s)for j in e(v)))for i in set(p(s))],key=lambda x:x[1]) #main test cases print(f('their')) print(f('their, her')) print(f('her, him')) print(f('she, he')) print(f('they, she, he')) print(f('them, her, him')) print(f('goca, oc, oca')) print(f('goca, soca, oc, goce, soce')) print(f('goca, soca, gsocae, goce, soce')) print(f('goca, soca, gsxocae, goce, soce, xoce'))
Standard input is empty
(('their',), 0) ((('th',), ('h',), 'e', ('ir',), ('r',)), -14) (('h', ('er',), ('im',)), -2) ((('she',), ('he',)), -2) (((('t',), ('s',), 'h', ('e', ('y',))), ('he',)), -22) ((('t',), 'h', ('e',), ('m',), ('r',), ('i',)), -30) ((((('g',), 'o'), 'c', ('a',)), ('oca',)), -10) (((('g',), ('s',), 'o', (('c',), 'a')), ('oc',), (('g',), ('s',), 'o', (('c',), 'e'))), -333) (((('g',), ('s',), 'o', (('c',), 'a')), ('gsocae',), (('g',), ('s',), 'o', (('c',), 'e'))), -333) ((('goca',), ((('g',), 's', ('x',)), 'o', (('c',), 'a', ('e',))), ('goce',), (('x',), ('s',), 'o', ('c', ('e',)))), -1346)