Skip to main content
1 of 2
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 497 bytes

lambda x:sum(f(x))
E=enumerate
S=sorted
u=lambda x:str(S(map(S,x)))
def f(a,p=1):
 if len(a)==1:yield p*(a[0][0]==a[0][1]);return
 d={}
 for i,j in E(a):
  if j[0]!=j[-1]:n=u(a[:i]+a[i+1:]);d[(n,0)]=d.get((n,0),0)+1
  for I,J in E(a):
   if i<I:
    F=[y for x,y in E(a)if x not in[i,I]]
    for j in[j,j[::-1]]:
     for J in[J,J[::-1]]:
      if j[-1]!=J[0]:n=u(F+[j[:-1]+J[1:]]);d[(n,1)]=d.get((n,1),0)+1
 if{}==d:yield p
 for x,y in d:
  if y:yield from f(eval(x),p*(d[(x,y)]/sum(d.values())))

Try it online!

Ajax1234
  • 7.7k
  • 1
  • 15
  • 28