Given three strings <code>a</code>, <code>b</code>, and <code>c</code>, your task is to find a string that has the<strong> minimum</strong> length and contains all three strings as <strong>substrings</strong>.
<p>If there are multiple such strings, return the<em> </em><strong>lexicographically<em> </em>smallest </strong>one.</p>

<p>Return <em>a string denoting the answer to the problem.</em></p>

<p><strong>Notes</strong></p>

<ul>
	<li>A string <code>a</code> is <strong>lexicographically smaller</strong> than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears <strong>earlier </strong>in the alphabet than the corresponding letter in <code>b</code>.</li>
	<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> a = &quot;abc&quot;, b = &quot;bca&quot;, c = &quot;aaa&quot;
<strong>Output:</strong> &quot;aaabca&quot;
<strong>Explanation:</strong>  We show that &quot;aaabca&quot; contains all the given strings: a = ans[2...4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and &quot;aaabca&quot; is the lexicographically smallest one.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> a = &quot;ab&quot;, b = &quot;ba&quot;, c = &quot;aba&quot;
<strong>Output:</strong> &quot;aba&quot;
<strong>Explanation: </strong>We show that the string &quot;aba&quot; contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that &quot;aba&quot; is the lexicographically smallest one.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= a.length, b.length, c.length &lt;= 100</code></li>
	<li><code>a</code>, <code>b</code>, <code>c</code> consist only of lowercase English letters.</li>
</ul>

class Solution:
    def minimumString(self, a: str, b: str, c: str) -> str:
        La=len(a)
        Lb=len(b)
        Lc=len(c)
        def add(x,y):
            Lx=len(x)
            Ly=len(y)
            for i in range(0,Lx):
                if i+Ly<=Lx and y==x[i:i+Ly]:
                    return x
                if Lx-i<=Ly and x[i:]==y[:Lx-i]:
                    return(x+y[Lx-i:])
            return x+y
        A=[add(a+b,c),add(a+c,b)]
        B=[add(b+a,c),add(b+c,a)]
        C=[add(c+a,b),add(c+b,a)]
        ML=min(len(A[0]),len(A[1]),len(B[0]),len(B[1]),len(C[0]),len(C[1]))
        AB=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
        for i in range(0,La):
            if i+Lb<=La and b==a[i:i+Lb]:
                y=add(a,c)
                A.append(y)
                if len(y)<ML:
                    ML=len(y)
            if La-i<=Lb and a[i:]==b[:La-i]:
                x=a+b[La-i:]
                y=add(x,c)
                A.append(y)
                if len(y)<ML:
                    ML=len(y)
            if i+Lc<=La and c==a[i:i+Lc]:
                y=add(a,b)
                A.append(y)
                if len(y)<ML:
                    ML=len(y)
            if La-i<=Lc and a[i:]==c[:La-i]:
                x=a+c[La-i:]
                y=add(x,b)
                A.append(y)
                if len(y)<ML:
                    ML=len(y)
        for i in range(0,Lb):
            if i+La<=Lb and a==b[i:i+La]:
                y=add(b,c)
                B.append(y)
                if len(y)<ML:
                    ML=len(y)
            if Lb-i<=La and b[i:]==a[:Lb-i]:
                x=b+a[Lb-i:]
                y=add(x,c)
                B.append(y)
                if len(y)<ML:
                    ML=len(y)
            if i+Lc<=Lb and c==b[i:i+Lc]:
                y=add(b,a)
                B.append(y)
                if len(y)<ML:
                    ML=len(y)
            if Lb-i<=Lc and b[i:]==c[:Lb-i]:
                x=b+c[Lb-i:]
                y=add(x,a)
                B.append(y)
                if len(y)<ML:
                    ML=len(y)
        for i in range(0,Lc):
            if i+La<=Lc and a==c[i:i+La]:
                y=add(c,b)
                C.append(y)
                if len(y)<ML:
                    ML=len(y)
            if Lc-i<=Lb and c[i:]==b[:Lc-i]:
                x=c+b[Lc-i:]
                y=add(x,a)
                C.append(y)
                if len(y)<ML:
                    ML=len(y)
            if i+Lb<=Lc and b==c[i:i+Lb]:
                y=add(c,a)
                C.append(y)
                if len(y)<ML:
                    ML=len(y)
            if Lc-i<=La and c[i:]==a[:Lc-i]:
                x=c+a[Lc-i:]
                y=add(x,b)
                C.append(y)
                if len(y)<ML:
                    ML=len(y)
        oldcand=A+B+C
        cand=[]
        for i in oldcand:
            if len(i)==ML:
                cand.append(i)
        def lex(List):
            if len(List)==1:
                return List[0]
            else:
                N=len(List[0])
                K=len(List)
                count=[0]*K
                for i in range(0,N):
                    for j in range(0,K):
                        num=AB.index(List[j][i])
                        count[j]+=num*(30**(N-i))
                    #print(count)
                    new=count.copy()
                    new.sort()
                    if new[0]<new[1]:
                        Mini=min(count)
                        ind=count.index(Mini)
                        return List[ind]
                Mini=min(count)
                ind=count.index(Mini)
                return List[ind]
        return lex(cand)
        
356