<p>An original string, consisting of lowercase English letters, can be encoded by the following steps:</p>

<ul>
	<li>Arbitrarily <strong>split</strong> it into a <strong>sequence</strong> of some number of <strong>non-empty</strong> substrings.</li>
	<li>Arbitrarily choose some elements (possibly none) of the sequence, and <strong>replace</strong> each with <strong>its length</strong> (as a numeric string).</li>
	<li><strong>Concatenate</strong> the sequence as the encoded string.</li>
</ul>

<p>For example, <strong>one way</strong> to encode an original string <code>&quot;abcdefghijklmnop&quot;</code> might be:</p>

<ul>
	<li>Split it as a sequence: <code>[&quot;ab&quot;, &quot;cdefghijklmn&quot;, &quot;o&quot;, &quot;p&quot;]</code>.</li>
	<li>Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes <code>[&quot;ab&quot;, &quot;12&quot;, &quot;1&quot;, &quot;p&quot;]</code>.</li>
	<li>Concatenate the elements of the sequence to get the encoded string: <code>&quot;ab121p&quot;</code>.</li>
</ul>

<p>Given two encoded strings <code>s1</code> and <code>s2</code>, consisting of lowercase English letters and digits <code>1-9</code> (inclusive), return <code>true</code><em> if there exists an original string that could be encoded as <strong>both</strong> </em><code>s1</code><em> and </em><code>s2</code><em>. Otherwise, return </em><code>false</code>.</p>

<p><strong>Note</strong>: The test cases are generated such that the number of consecutive digits in <code>s1</code> and <code>s2</code> does not exceed <code>3</code>.</p>

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

<pre>
<strong>Input:</strong> s1 = &quot;internationalization&quot;, s2 = &quot;i18n&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> It is possible that &quot;internationalization&quot; was the original string.
- &quot;internationalization&quot; 
  -&gt; Split:       [&quot;internationalization&quot;]
  -&gt; Do not replace any element
  -&gt; Concatenate:  &quot;internationalization&quot;, which is s1.
- &quot;internationalization&quot;
  -&gt; Split:       [&quot;i&quot;, &quot;nternationalizatio&quot;, &quot;n&quot;]
  -&gt; Replace:     [&quot;i&quot;, &quot;18&quot;,                 &quot;n&quot;]
  -&gt; Concatenate:  &quot;i18n&quot;, which is s2
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;l123e&quot;, s2 = &quot;44&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> It is possible that &quot;leetcode&quot; was the original string.
- &quot;leetcode&quot; 
  -&gt; Split:      [&quot;l&quot;, &quot;e&quot;, &quot;et&quot;, &quot;cod&quot;, &quot;e&quot;]
  -&gt; Replace:    [&quot;l&quot;, &quot;1&quot;, &quot;2&quot;,  &quot;3&quot;,   &quot;e&quot;]
  -&gt; Concatenate: &quot;l123e&quot;, which is s1.
- &quot;leetcode&quot; 
  -&gt; Split:      [&quot;leet&quot;, &quot;code&quot;]
  -&gt; Replace:    [&quot;4&quot;,    &quot;4&quot;]
  -&gt; Concatenate: &quot;44&quot;, which is s2.
</pre>

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

<pre>
<strong>Input:</strong> s1 = &quot;a5b&quot;, s2 = &quot;c5b&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible.
- The original string encoded as s1 must start with the letter &#39;a&#39;.
- The original string encoded as s2 must start with the letter &#39;c&#39;.
</pre>

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

<ul>
	<li><code>1 &lt;= s1.length, s2.length &lt;= 40</code></li>
	<li><code>s1</code> and <code>s2</code> consist of digits <code>1-9</code> (inclusive), and lowercase English letters only.</li>
	<li>The number of consecutive digits in <code>s1</code> and <code>s2</code> does not exceed <code>3</code>.</li>
</ul>

class Solution:
    def possiblyEquals(self, s1: str, s2: str) -> bool:
        N = len(s1)
        M = len(s2)
        @cache
        def find(s,i,n):
            if i+2<n and s[i:i+3].isnumeric():
                return [int(s[i]) + int(s[i+1]) +int(s[i+2]),int(s[i:i+2]) +int(s[i+2]) , int(s[i]) + int(s[i+1:i+3]), int(s[i:i+3])] , i+3
            if i+1<n and s[i:i+2].isnumeric():
                return [int(s[i]) + int(s[i+1]), int(s[i:i+2])], i+2
            if i<n and s[i].isnumeric():
                return [int(s[i])] , i+1

        @cache
        def solve(i,j,I,J):
            d = min(I,J)
            I -= d
            J -= d

            print(i,j,I,J)
            if i ==N and j==M:
                return I==J

            if i==N:
                if I==0:
                    return j==M and J==0
                elif J>0:
                    return solve(i,j,I-1,J-1)
                elif s2[j].isalpha():
                    return solve(i,j+1,I-1,J)
                else:
                    possibles,nxt = find(s2,j,M)
                    val = False
                    for poss in possibles:
                        val = val or solve(i,nxt,I,J+poss)
                    return val
                
                    


                

            if j==M:
                if J==0:
                    return i==N and I==0
                elif I>0:
                    return solve(i,j,I-1,J-1)
                elif s1[i].isalpha():
                    return solve(i+1,j,I,J-1)
                else:
                    possibles,nxt = find(s1,i,N)
                    val = False
                    for poss in possibles:
                        val = val or solve(nxt,j,I+poss,J)
                    return val

            if s1[i].isalpha() and s2[j].isalpha():
                if I==J:
                    if s1[i]==s2[j]:
                        return solve(i+1,j+1,I,J)
                    else:
                        return False
                elif I>J:
                    return solve(i,j+1,I-1,J)
                else:
                    return solve(i+1,j,I,J-1)

            elif s1[i].isalpha():
                if J>0:
                    return solve(i+1,j,I,J-1)
                possibles,nxt = find(s2,j,M)
                val = False
                for poss in possibles:
                    val = val or solve(i,nxt,I,J+poss)
                return val
            elif s2[j].isalpha():
                if I>0:
                    return solve(i,j+1,I-1,J)
                possibles,nxt = find(s1,i,N)
                val = False
                for poss in possibles:
                    val = val or solve(nxt,j,I+poss,J)
                return val
            else:
                p1,i1 = find(s1,i,N)
                p2,i2 = find(s2,j,M)
                val = False
                for a in p1:
                    for b in p2:
                        val = val or solve(i1,i2,I+a,J+b)

                return val 


        # print(find(s2,0,M))


        
        return solve(0,0,0,0)
1307