696. Count Binary Substrings

本文介绍了一种算法,用于解决寻找具有相等数量连续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的数量相同,且0和1分别连续。

解题思路:计算连续0或者1的数量number1,接着数值变化了以后,先记录此时的位置position,同时计算连续1或者0的数量number2,此时子串的数量为number1和number2的较小值。从记录的位置position开始。

class Solution {
    public int countBinarySubstrings(String s) {
                        int count = 0;
                        int total = 1;//解题思路中的number1
                        int sum = 0;//解题思路中的number2
                        int begin = 0;//用来记录开始的位置
                        char[] sArray = s.toCharArray();
                        if(sArray.length == 1) return 0;
                        int judge = sArray[0];
                        for(int i = 1; i < sArray.length; ++i){
                            if( sArray[i] == judge)  ++total;
                            if( sArray[i] != judge && sArray[i-1] == judge ){
                                begin = i;
                                for(;i < sArray.length && sArray[i] !=judge; ++i){
                                    count ++;
                                }
                                sum += Math.min(total,count);
                                total = 1;
                                count = 0;
                                i = begin;
                                judge = sArray[i];
                            }

                        }
                    return sum;
}
}

改进: 假设num1个连续出现的0与num2个连续出现的1相邻,则这个字符串的中子串的数量就是num1与num2的较小值。比如00111的子串大小就是2和3中的较小值。之前的解法重复计算了连续0或1的个数。改进代码如下:

class Solution {
    public int countBinarySubstrings(String s) {
                        int num1 = 1;
                        int num2 = 0;
                        int sum = 0;
                        int i = 1;
                        char[] sArray = s.toCharArray();
                        if(sArray.length == 1) return 0;
                        char judge = sArray[0];
                        for(  ; i < sArray.length && sArray[i] == judge ; ++i){
                            ++ num1;
                        }
                        for( ; i < sArray.length ; ++ i ){
                            if(sArray[i] != judge) num2++;
                            if(sArray[i] == judge && sArray[i-1] != judge){
                                sum += Math.min(num1 , num2);
                                num1 = num2;
                                num2 = 1;
                                judge = sArray[i-1];
                            }
                            if(i == sArray.length - 1) sum += Math.min(num1, num2);
                        }           
                        return sum;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值