Skip to content

Practice Maximize Number of Subsequences in a String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2207
LeetCode Problem

Maximize Number of Subsequences in a String

Problem Statement

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.

Source: Maximize Number of Subsequences in a String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
text = "abdcdbc", pattern = "ac"
Output
4

# Example 2

Input
text = "aabb", pattern = "ab"
Output
6

Constraints

1 <= text.length <= 10^5
pattern.length == 2
text and pattern consist only of lowercase English letters.

Related LeetCode Problems

Practice more problems similar to Maximize Number of Subsequences in a String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.