LeetCode 696. Count Binary Substrings

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‘

题意:给定一个由0和1组成的非空字符串,计算出由相同数量的0和1、且0和1分别连续的子串的个数。思路1:直接暴力解决,但时间复杂度高,提交后time limit exceeded。从前往后遍历,判断当前数字的连续相同数字的数量,再找出从第一个不同数字开始,连续不同数字的数量,数量相同则count加1,否则count不变。代码如下:
public int countBinarySubstrings(String s) {
        char[] chars = s.toCharArray();
        int count = 0;
        for(int i = 0; i < chars.length - 1; i++){
            if(isSubstrings(chars, i)){
                count++;
            }
        }
        return count;
    }
    public boolean isSubstrings(char[] chars, int start){
        int same = start + 1;
        while(same < chars.length - 1 && chars[start] == chars[same]){
            same++;
        }
        int diff = same;
        while(diff < chars.length && chars[diff] != chars[start] && diff - same < same - start){
            diff++;
        }
        return diff - same == same - start ? true : false;
    }

思路2:(LeetCode提供的算法1)在字符串s中,统计相同数字连续出现了多少次。例:s = "00110",则group = {2,2,1}。这样,在统计有多少个满足条件的子串时,对group数组从前往后遍历,依次取min{group[i], group[i+1]},再求和即可。

原因:以group[i]=2, group[i+1]=3为例,则表示“00111”或“11000”。
以“00111”为例,group={2,3},当第一个1出现时,前面已经有2个0了,所以肯定能组成01;再遇到下一个1时,此时有2个0,2个1,所以肯定能组成0011;再遇到下一个1时,前面只有2个0,而此时有3个1,所以不可以再组成满足条件的子串了。

代码如下:

public int contBinarySubstrings(String s){
        char[] chars = s.toCharArray();
        int[] group = new int[chars.length];
        int index = 0;
        group[0] = 1;
        for(int i = 1; i < chars.length; i++){
            if(chars[i] == chars[i - 1])
                group[index]++;
            else
                group[++index] = 1;
        }
        int i = 0, count = 0;
        while(i < group.length - 1 && group[i] != 0){
            count += Math.min(group[i], group[i + 1]);
            i++;
        }
        return count;
    }

思路3:(LeetCode提供的算法2)定义两个变量pre和cur,pre存储当前数字前的数字连续次数,cur存储当前数字的连续次数。然后从第二个数字开始遍历,如果当前数字与前面数字相同,则cur自增1;否则对pre和cur取最小值,记为子串次数,并将pre赋值为cur,cur重置为1。原理与上一个方法相同,但没有定义数组,空间复杂度降低。

代码如下:



public int contBinarySubstring(String s){
        int pre = 0, cur = 1, count = 0;
        for(int i = 1; i < s.length(); i++){
            if(s.charAt(i) != s.charAt(i - 1)){
                count += Math.min(pre, cur);
                pre = cur;
                cur = 1;
            }
            else{
                cur++;
            }
        }
        return count + Math.min(pre, cur);
    }

 
时间: 2024-10-02 20:20:35

LeetCode 696. Count Binary Substrings的相关文章

696. Count Binary Substrings - LeetCode

Question 696. Count Binary Substrings 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&qu

[LeetCode&amp;Python] Problem 696. Count Binary Substrings

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 numbe

LeetCode算法题-Count Binary Substrings(Java实现)

这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串的数量,并且这些子串中的所有0和所有1都是连续的.重复出现的子串也计算在内.例如: 输入:"00110011" 输出:6 说明:有6个子串具有相同数量的连续1和0:"0011","01","1100","10"

[leetcode] Count Binary Substrings

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 numbe

leetcode 696

696. Count Binary Substrings 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&qu

CodeForces 451D Count Good Substrings

哎,最近都在做图论,没有练DP,现在一遇到DP就不会了= = 因为有合并这个操作,所以只要是首位相同的字符串肯定是能够构成good串的,那么只要统计在奇数位上出现的0,1的个数和偶数位数,随便递推一下就出来了 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #i

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode:Count and Say

1.题目名称 Count and Say(按照数字重复出现计数并生成字符串) 2.题目地址 https://leetcode.com/problems/count-and-say/ 3.题目内容 英文:The count-and-say sequence is the sequence of integers beginning as follows 中文:给出正整数n,返回"count-and-say"序列的第n项 说明: count-and-say序列形如:1, 11, 21, 1