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", 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.

Solution

思路:题目大意是,给一个二进制的字符串,问有多少子串的0个数量等于1的数量且子串中0和1不能交替出现。

00110011
  zeroCount oneCount last
0 1         0         -1->0
0 2         0         0
1 2         1         0->1      有一个符合要求的子串01
1 2         2         1         有一个符合要求的子串0011
0 1         2         1->0      有一个符合要求的子串10
0 2         2         0         有一个符合要求的子串1100
1 2         1         0->1      有一个符合要求的子串01
1 2         2         1         有一个符合要求的子串0011

Java实现:

public int countBinarySubstrings(String s) {
    // 子串满足
    // 1)0和1个数相等
    // 2)0与1不能交替出现

    int ans = 0;
    int zeroCount = 0;
    int oneCount = 0;
    int last = -1;
    for (int i=0; i<s.length(); i++) {
        char c = s.charAt(i);
        if (c == '0') {
            if (last == -1 || last == 1) {
                zeroCount = 0;
                last = 0;
            }
            zeroCount++;
        } else {
            if (last == -1 || last == 0) {
                oneCount = 0;
                last = 1;
            }
            oneCount++;
        }
        if (last == 0) ans += zeroCount<=oneCount?1:0;
        else ans += oneCount<=zeroCount?1:0;
    }
    return ans;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9245719.html

时间: 2024-11-05 19:37:28

696. Count Binary Substrings - LeetCode的相关文章

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 numbe

[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

CodeForces 451D Count Good Substrings

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

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Count and Say leetcode java

题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Gi

Maximum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

Balanced Binary Tree leetcode java

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题解: 采用递归的方法,要记录depth用来比较. 代码如下: