LeetCode-696-Count Binary Substringss-E

本文介绍了一种算法,用于计算一个由0和1组成的字符串中满足特定条件的子串数量。这些子串需包含等量且连续的0和1,并能重复出现。文章详细解释了解决方案并提供了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
Example 1:

Input: “00110011”
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1’s and 0’s: “0011”, “01”, “1100”, “10”, “0011”, and “01”.

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, “00110011” is not a valid substring because all the 0’s (and 1’s) are not grouped together.

Example 2:

Input: “10101”
Output: 4
Explanation: There are 4 substrings: “10”, “01”, “10”, “01” that have equal number of consecutive 1’s and 0’s.

Note:

s.length will be between 1 and 50,000.
s will only consist of “0” or “1” characters.

一个由0和1组成字符串,找出它的满足以下条件的子串的个数:
(1)0和1的个数一样 (2)所有的0必须连续,所有的1必须连续
(3)这些满足条件的子串互相之间可以重复

解题思路:创建一个数组,这个数组用来存储所有连续的1和连续的0的个数,例如”111001111”字符串对应到这个数组存储的元素就是”3 2 4”,然后依次比较这个数组中相邻两个数,获得那个较小的数字。把数组中n个元素中n-1个比较出来得到最小数加在一起,就是最后的结果。
这是因为在连续的m个0紧接着连续的n个1形成的字符串中,可以形成的符合要求的子串个数共有min(m,n)个。
以111001111为例:11100可以形成min(3,2)=2个符合要求的子串,001111可以形成min(2,4)=2个符合要求的子串,所以共有4个符合要求的子串。

int countBinarySubstrings(string s) {
        vector<int>consecutiveNo;
        int n=s.size();
        int count=1;
        for(int i=1;i<=n;i++){
            if(s[i]==s[i-1]){
                count++;
            }else{
                consecutiveNo.push_back(count);
                count=1;
            }
        }
        int res=0;
        for(int j=1;j<consecutiveNo.size();j++){
            res+=min(consecutiveNo[j],consecutiveNo[j-1]);
        }
        return res;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值