# 2207. 字符串中最多数目的子字符串

### 题目地址(6021. 字符串中最多数目的子字符串)

<https://leetcode-cn.com/problems/maximize-number-of-subsequences-in-a-string/>

### 题目描述

```
给你一个下标从 0 开始的字符串 text 和另一个下标从 0 开始且长度为 2 的字符串 pattern ，两者都只包含小写英文字母。

你可以在 text 中任意位置插入 一个 字符，这个插入的字符必须是 pattern[0] 或者 pattern[1] 。注意，这个字符可以插入在 text 开头或者结尾的位置。

请你返回插入一个字符后，text 中最多包含多少个等于 pattern 的 子序列 。

子序列 指的是将一个字符串删除若干个字符后（也可以不删除），剩余字符保持原本顺序得到的字符串。

 

示例 1：

输入：text = "abdcdbc", pattern = "ac"
输出：4
解释：
如果我们在 text[1] 和 text[2] 之间添加 pattern[0] = 'a' ，那么我们得到 "abadcdbc" 。那么 "ac" 作为子序列出现 4 次。
其他得到 4 个 "ac" 子序列的方案还有 "aabdcdbc" 和 "abdacdbc" 。
但是，"abdcadbc" ，"abdccdbc" 和 "abdcdbcc" 这些字符串虽然是可行的插入方案，但是只出现了 3 次 "ac" 子序列，所以不是最优解。
可以证明插入一个字符后，无法得到超过 4 个 "ac" 子序列。


示例 2：

输入：text = "aabb", pattern = "ab"
输出：6
解释：
可以得到 6 个 "ab" 子序列的部分方案为 "aaabb" ，"aaabb" 和 "aabbb" 。


 

提示：

1 <= text.length <= 105
pattern.length == 2
text 和 pattern 都只包含小写英文字母。
```

### 前置知识

*

### 公司

* 暂无

### 思路

首先如果题目直接让求 text 中有多少 pattern 子序列，那么可以通过一次遍历求出。

对于每个位置 i，我们计算出以其结束（开始也行）的 pattern 子序列有多少，累加起来 就是答案。

代码：

```py
class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
        a = b = ans = 0
        for c in text:
            if c == pattern[1]:
                b += 1
                ans += a # 这里累加答案。含义为以当前位置结尾的子序列有 a 个，因此累加上 a
            if c == pattern[0]:
                a += 1
        return ans
```

由于我们可以插入一次，那么实际上最优：

* 可以插入一个 pattern\[0] 在 text 前面，这样多 b 个子序列。
* 可以插入一个 pattern\[1] 在 text 后面，这样多 a 个子序列。

a 和 b 取较大值即可。

### 关键点

*

### 代码

* 语言支持：Python3

Python3 Code:

```python

class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
        a = b = ans = 0
        for c in text:
            if c == pattern[1]:
                b += 1
                ans += a
            if c == pattern[0]:
                a += 1
        return ans + max(a, b)

```

**复杂度分析**

令 n 为数组长度。

* 时间复杂度：$O(n)$
* 空间复杂度：$O(1)$

> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。

力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/)，这样就会第一时 间收到我的动态啦\~

以上就是本文的全部内容了。大家对此有何看法，欢迎给我留言，我有时间都会一一查看回 答。更多算法套路可以访问我的 LeetCode 题解仓库 ：<https://github.com/azl397985856/leetcode> 。 目前已经 40K star 啦。大家也可以关 注我的公众号《力扣加加》带你啃下算法这块硬骨头。

关注公众号力扣加加，努力用清晰直白的语言还原解题思路，并且有大量图解，手把手教你 识别套路，高效刷题。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/medium/6201.maximize-number-of-subsequences-in-a-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
