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

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

Return the maximum number of times pattern can occur as a subsequence of the modified text.

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

&nbsp;
Example 1:

Input: text = "abdcdbc", pattern = "ac"
Output: 4
Explanation:
If we add pattern[0] = 'a' in between text[1] and text[2], we get "abadcdbc". Now, the number of times "ac" occurs as a subsequence is 4.
Some other strings which have 4 subsequences "ac" after adding a character to text are "aabdcdbc" and "abdacdbc".
However, strings such as "abdcadbc", "abdccdbc", and "abdcdbcc", although obtainable, have only 3 subsequences "ac" and are thus suboptimal.
It can be shown that it is not possible to get more than 4 subsequences "ac" by adding only one character.


Example 2:

Input: text = "aabb", pattern = "ab"
Output: 6
Explanation:
Some of the strings which can be obtained from text and have 6 subsequences "ab" are "aaabb", "aaabb", and "aabbb".


&nbsp;
Constraints:


	1 &lt;= text.length &lt;= 105
	pattern.length == 2
	text and pattern consist only of lowercase English letters.


class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
        total = count_a = count_b = 0
        for c in text:
            if c == pattern[1]:
                total += count_a
                count_b += 1
            if c == pattern[0]:
                count_a += 1
        
        return total + max(count_a, count_b)
class Solution {
    public long maximumSubsequenceCount(String text, String pattern) {
        //when pattern[0] == pattern[1]
        if (pattern.charAt(0) == pattern.charAt(1)) {
            long freq = 1;
            //O(N)
            for (int i = 0; i < text.length(); i++) {
                if (text.charAt(i) == pattern.charAt(0)) {
                    freq++;
                }
            }
            //number of subsequences : choose any two characters from freq nC2
            return (freq * (freq - 1)) / 2;
        }

        //choice 1
        String text1 = pattern.charAt(0) + text;

        int freq = 0;
        long count1 = 0;
        //O(N)
        for (int i = 0; i < text1.length(); i++) {
            if (text1.charAt(i) == pattern.charAt(0)) {
                freq++;
            } else if (text1.charAt(i) == pattern.charAt(1)) {
                count1 += freq;
            }
        }

        //choice 2
        String text2 = text + pattern.charAt(1);
        freq = 0;
        long count2 = 0;
        //O(N)
        for (int i = text2.length() - 1; i>= 0; i--) {
            if (text2.charAt(i) == pattern.charAt(1)) {
                freq++;
            } else if (text2.charAt(i) == pattern.charAt(0)) {
                count2 += freq;
            }
        }

        return Math.max(count1, count2);
    }
}
class Solution {
public:
    long long maximumSubsequenceCount(string text, string pattern) {
        // support variables
        int len = text.size();
        long long res = 0, aCount = 0, bCount = 0;
        char a = pattern[0], b = pattern[1];
        // getting the frequencies
        for (char c: text)  {
            if (c == a) aCount++;
            else if (c == b) bCount++;
        }
        // edge case: a == b
        if (a == b) return aCount++ * aCount / 2;
        // adding our extra character to maximise the occurrences
        if (aCount < bCount) res += bCount;
        else bCount++;
        // computing the occurrences
        for (char c: text)  {
            // first case: spotting the first element of a sequence
            if (c == a) {
                res += bCount;
            }
            // second case: we found an ending sequence
            else if (c == b) bCount--;
            // all the rest: we do nothing
        }
        return res;
    }
};
/**
 * @param {string} text
 * @param {string} pattern
 * @return {number}
 */
var maximumSubsequenceCount = function(text, pattern) {
    const arrText = text.split("")
    const lengthP0 = arrText.filter(x => x === pattern[0]).length
    const lengthP1 = arrText.filter(x => x === pattern[1]).length
    const [c1, c2, lengthmax] = lengthP0 <= lengthP1 ? [...pattern, lengthP1]: [pattern[1], pattern[0], lengthP0]
    let newText = lengthP0 <= lengthP1 ? [c1,...arrText]: [...arrText, c1]
    newText = lengthP0 <= lengthP1 ? newText : newText.reverse()

    let i = 0;
    let count = 0;
    let countmax = lengthmax
    while(i < newText.length) {
        if(newText[i] === c1) {
            count += countmax
        }
        if(newText[i] === c2) {
            countmax--
        }
        i++;
    }
    return count
};
Maximize Number of Subsequences in a String