<p>You are given a <strong>0-indexed</strong> string <code>text</code> and another <strong>0-indexed</strong> string <code>pattern</code> of length <code>2</code>, both of which consist of only lowercase English letters.</p>

<p>You can add <strong>either</strong> <code>pattern[0]</code> <strong>or</strong> <code>pattern[1]</code> anywhere in <code>text</code> <strong>exactly once</strong>. Note that the character can be added even at the beginning or at the end of <code>text</code>.</p>

<p>Return <em>the <strong>maximum</strong> number of times</em> <code>pattern</code> <em>can occur as a <strong>subsequence</strong> of the modified </em><code>text</code>.</p>

<p>A <b>subsequence</b> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>

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

<pre>
<strong>Input:</strong> text = &quot;abdcdbc&quot;, pattern = &quot;ac&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong>
If we add pattern[0] = &#39;a&#39; in between text[1] and text[2], we get &quot;ab<u><strong>a</strong></u>dcdbc&quot;. Now, the number of times &quot;ac&quot; occurs as a subsequence is 4.
Some other strings which have 4 subsequences &quot;ac&quot; after adding a character to text are &quot;<u><strong>a</strong></u>abdcdbc&quot; and &quot;abd<u><strong>a</strong></u>cdbc&quot;.
However, strings such as &quot;abdc<u><strong>a</strong></u>dbc&quot;, &quot;abd<u><strong>c</strong></u>cdbc&quot;, and &quot;abdcdbc<u><strong>c</strong></u>&quot;, although obtainable, have only 3 subsequences &quot;ac&quot; and are thus suboptimal.
It can be shown that it is not possible to get more than 4 subsequences &quot;ac&quot; by adding only one character.
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;aabb&quot;, pattern = &quot;ab&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong>
Some of the strings which can be obtained from text and have 6 subsequences &quot;ab&quot; are &quot;<u><strong>a</strong></u>aabb&quot;, &quot;aa<u><strong>a</strong></u>bb&quot;, and &quot;aab<u><strong>b</strong></u>b&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= text.length &lt;= 10<sup>5</sup></code></li>
	<li><code>pattern.length == 2</code></li>
	<li><code>text</code> and <code>pattern</code> consist only of lowercase English letters.</li>
</ul>

class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:

        # SHORTCUT: all letters that are neither A nor B may be ignored entirely.
        (A, B) = pattern
        relevant_text = [
            L
            for L in text
            if L in (A, B)
        ]
        # print(f'{relevant_text=}')

        # SHORTCUT: the greatest values will be from putting
        # pattern[0] ("A" on the FRONT, or pattern[1] ("B") on the END.
        # All other values will be either equal or less than these.

        testA = [A] + relevant_text
        testB = relevant_text + [B]

        def show_big_list(L: List[str]) -> str:
            if len(L) < 10:
                return f'{L}'
            else:
                return f'{L[:5]}...({len(L)-10})...{L[-5:]}'

        # SHORTCUT: the value of a string will equal:
        # for each letter B: the sum of all letter A to the left of it.

        def valueOf_IfDifferent(test: List[str]) -> int:
            print(f'valueOf({show_big_list(test)}): [DIFF]')
            ones_at_A = [
                1 if (L == A) else 0
                for L in test
            ]
            print(f'  ones_at_A={show_big_list(ones_at_A)}')
            A_left_of = list(accumulate(ones_at_A))
            print(f'  A_left_of={show_big_list(A_left_of)}')
            sums_at_B = [
                Acount if (L == B) else 0
                for L, Acount in zip(test, A_left_of)
            ]
            print(f'  sums_at_B={show_big_list(sums_at_B)}')
            return sum(sums_at_B)

        # If A == B, I think the above still holds, but alternately
        # we can also calculate this as (number of A's) pick (2)
        # which equals:

        def Nchoose2(N: int) -> int:
            return N * (N - 1) // 2

        def valueOf_IfSame(test: List[str]) -> int:
            print(f'valueOf({show_big_list(test)}): [SAME]')
            return Nchoose2(len(test))
        
        def valueOf(test: List[str]) -> int:
            if A == B:
                return valueOf_IfSame(test)
            else:
                return valueOf_IfDifferent(test)

        answerA = valueOf(testA)
        answerB = valueOf(testB)
        print(f'{answerA=}, {answerB=}')
        return max(answerA, answerB)

247