You are given a string text and a two-character pattern. Your goal is to maximize the number of subsequences that match the pattern by strategically adding exactly one character to the text.
You can add either pattern[0] or pattern[1] anywhere in the text - at the beginning, end, or any position in between. The challenge is to determine which character to add and where to place it for maximum impact.
What is a subsequence? A subsequence preserves the relative order of characters but doesn't need to be contiguous. For example, in "abc", the subsequences include "a", "b", "c", "ab", "ac", "bc", and "abc".
Example: If text = "abdcdbc" and pattern = "ac", adding 'a' at the beginning gives us "aabdcdbc", which contains 4 subsequences of "ac".
Input & Output
Visualization
Time & Space Complexity
Single pass through the string to count characters and subsequences
Only using constant extra space for counters
Constraints
- 1 โค text.length โค 105
- pattern.length == 2
- text and pattern consist only of lowercase English letters