2207.字符串中最多数目的子序列
链接:2207.字符串中最多数目的子序列
难度:Medium
标签:贪心、字符串、前缀和
简介:请你返回插入一个字符后,text 中最多包含多少个等于 pattern 的 子序列 。
题解 1 - python
- 编辑时间:2024-09-24
- 执行用时:162ms
- 内存消耗:16.99MB
- 编程语言:python
- 解法介绍:遍历时统计两个字符的数量
class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
        cnt1 = cnt2 = res = 0
        for c in text:
            if c == pattern[1]:
                res += cnt1
                cnt2 += 1
            if c == pattern[0]:
                cnt1 += 1
        return res + max(cnt1, cnt2)