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:
        a, b, c = list(a), list(b), list(c)
        cases = [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]]
        res = []
        m = float('inf')
        
        for s1, s2, s3 in cases:
            new_s = None
            if (len(s1) <= len(s2)):
                for i in range(len(s2)-len(s1)+1):
                    if (s1 == s2[i:i+len(s1)]):
                        new_s = s2
                        break
            else:
                for i in range(len(s1)-len(s2)+1):
                    if (s2 == s1[i:i+len(s1)]):
                        new_s = s1
                        break
            
            if not new_s:
                for i in range(len(s1)):
                    suffix = s1[i:]
                    if (len(suffix) > len(s2)):
                        continue
                    prefix = s2[:len(suffix)]
                    if (suffix == prefix):
                        new_s = s1[:i] + suffix + s2[len(suffix):]
                        break
            
            if not new_s:
                new_s = s1 + s2
                
            fin_s = None
            if (len(new_s) <= len(s3)):
                for i in range(len(s3)-len(new_s)+1):
                    if (new_s == s3[i:i+len(new_s)]):
                        fin_s = s3
                        break
            else:
                for i in range(len(new_s)-len(s3)+1):
                    if (s3 == new_s[i:i+len(s3)]):
                        fin_s = new_s
                        break
            
            if not fin_s:
                for i in range(len(new_s)):
                    suffix = new_s[i:]
                    if (len(suffix) > len(s3)):
                        continue
                    prefix = s3[:len(suffix)]
                    if (suffix == prefix):
                        fin_s = new_s[:i] + suffix + s3[len(suffix):]
                        break
            
            if not fin_s:
                fin_s = new_s + s3
            
            if (len(fin_s) < m):
                res = [''.join(fin_s)]
                m = len(fin_s)
            elif (len(fin_s) == m):
                res.append(''.join(fin_s))
            
                        
        res.sort()

        return res[0]

        
        
        
        
369