Maximize Number of Subsequences in a String - Problem

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

example_1.py โ€” Basic Case
$ Input: text = "abdcdbc", pattern = "ac"
โ€บ Output: 4
๐Ÿ’ก Note: Adding 'a' at the beginning creates "aabdcdbc". This gives us 4 subsequences of "ac": positions (0,3), (0,4), (1,3), (1,4) where we can pick 'a' and then 'c'.
example_2.py โ€” Same Characters
$ Input: text = "aabb", pattern = "ab"
โ€บ Output: 6
๐Ÿ’ก Note: Adding 'a' at the start gives "aaabb" with 3ร—2=6 subsequences of "ab". Adding 'b' at the end gives "aabbb" with 2ร—3=6 subsequences. Both are optimal.
example_3.py โ€” Identical Pattern
$ Input: text = "aa", pattern = "aa"
โ€บ Output: 3
๐Ÿ’ก Note: With identical pattern characters, we add one more 'a' to get "aaa". The number of ways to choose 2 'a's from 3 is C(3,2) = 3.

Visualization

Tap to expand
Strategic Placement VisualizationScenario: Maximizing HandshakesStudents A can only shake hands with students CCurrent Line: A-B-D-C-D-B-CABDCDBCCurrent handshakes: 2Strategy 1: Add A at startAABCCTotal: 4 handshakes (+2)Strategy 2: Add C at endCCCTotal: 3 handshakes (+1)Winner: Strategy 1Adding A at start = 4 totalNew A can shake hands withall existing C students
Understanding the Visualization
1
Survey the Line
Count how many students of each type are already in line
2
Calculate Opportunities
Determine handshake potential for each placement strategy
3
Optimal Placement
Choose the position that creates the most new handshake pairs
Key Takeaway
๐ŸŽฏ Key Insight: Always choose the placement that maximizes interactions between the new character and existing characters of the opposite type in the pattern!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string to count characters and subsequences

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค text.length โ‰ค 105
  • pattern.length == 2
  • text and pattern consist only of lowercase English letters
Asked in
Google 23 Amazon 15 Meta 12 Microsoft 8
18.2K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen