<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:
        # https://www.youtube.com/watch?v=K6d524jDH3A
        def parse(s):
            v = []
            i, n = 0, len(s)
            while i < n:
                if s[i].isalpha():
                    v.append(s[i])
                    i += 1
                else:
                    j = i
                    while j < n and s[j].isdigit():
                        j += 1
                    v.append(s[i:j])
                    i = j
            return v
        def getNum(string):
            if len(string)==1:
                v = [int(string)]
            if len(string)==2:
                num = int(string)
                v = [num%10+num//10, num]
            if len(string)==3:
                num = int(string)
                a = num//100
                b = num%100 // 10
                c = num%10
                v = [a+b+c, 10*a+b+c, a+10*b+c, num]
            return set(v)
        s1, s2 = parse(s1), parse(s2)
        n1, n2 = len(s1), len(s2)
        memo = {}
        def dfs(idx1, cnt1, idx2, cnt2):
            key = '_'.join([str(idx1), str(cnt1), str(idx2), str(cnt2)])
            if key in memo:
                return memo[key]
            if idx1==n1 and idx2==n2:
                return cnt1 == cnt2
            if idx1==n1 and cnt1==0:
                memo[key] = False
                return False
            if idx2==n2 and cnt2==0:
                memo[key] = False
                return False
            if idx1 < n1 and s1[idx1].isdigit():
                set1 = getNum(s1[idx1])
                for num in set1:
                    if dfs(idx1+1, cnt1+num, idx2, cnt2):
                        return True
                memo[key] = False
                return False
            if idx2 < n2 and s2[idx2].isdigit():
                set2 = getNum(s2[idx2])
                for num in set2:
                    if dfs(idx1, cnt1, idx2+1, cnt2+num):
                        return True
                memo[key] = False
                return False
            if cnt1 > 0 and cnt2 > 0:
                common = min(cnt1, cnt2)
                return dfs(idx1, cnt1-common, idx2, cnt2-common)
            if cnt1 > 0 and cnt2 == 0:
                return dfs(idx1, cnt1-1, idx2+1, 0)
            if cnt1 == 0 and cnt2 > 0:
                return dfs(idx1+1, 0, idx2, cnt2-1)
            if cnt1 == 0 and cnt2 == 0:
                if s1[idx1] != s2[idx2]:
                    memo[key] = False
                    return False
                return dfs(idx1+1, cnt1, idx2+1, cnt2)
        return dfs(0, 0, 0, 0)
1176