day-60 字符串中最多数目的子序列

在这里插入图片描述
思路
由题目可以得出,当字符串开头插入pattern[0]或在字符串结尾插入pattern[1]这两种情况中的一种所得到的子序列数目一定是最多的

解题过程
我们可以遍历字符串,统计pattern[0]的个数,每当遇到一个pattern[1]时,序列数就会加上已经遍历的pattern[0]的个数,最后加上Math.max(l,r)

Code

class Solution {
    public long maximumSubsequenceCount(String text, String pattern) {
        int len=text.length();
         int l=0,r=0;
         long ans=0;
        for(int i=0;i<len;i++){
            if(text.charAt(i)==pattern.charAt(0)){
               if(pattern.charAt(0)==pattern.charAt(1)) {
                   ans+=l;
               }
               l++;
               
            }else if(text.charAt(i)==pattern.charAt(1)){
               r++;
               ans+=l;
            }
        }
        return ans+Math.max(l,r);
    }
}

作者:菜卷
链接:https://leetcode.cn/problems/maximize-number-of-subsequences-in-a-string/solutions/2930151/zi-fu-chuan-zhong-zui-duo-shu-mu-de-zi-x-0bna/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值