原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/

题目:

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.

题解:

Count consecutive 0s or 1s.

e.g. 0001111, 就是3个0, 4个1.

When current pointing char is different. Add the min(preCount, curCount) to res.

Time Complexity: O(s.length()). Space: O(1).

AC Java:

 class Solution {
public int countBinarySubstrings(String s) {
if(s == null || s.length() == 0){
return 0;
} int n = s.length();
int preCount = 0;
int curCount = 0;
int res = 0; int i = 0;
while(i<n){
char cur = s.charAt(i);
while(i<n && s.charAt(i) == cur){
curCount++;
i++;
} res += Math.min(preCount, curCount);
preCount = curCount;
curCount = 0;
} return res;
}
}

类似Encode and Decode Strings.

LeetCode Count Binary Substrings的更多相关文章

  1. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  2. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  3. 696. Count Binary Substrings - LeetCode

    Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...

  4. 【Leetcode_easy】696. Count Binary Substrings

    problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...

  5. LeetCode 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  6. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

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

    这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...

  8. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  9. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

随机推荐

  1. Redis慢查询,redis-cli,redis-benchmark,info

    一.慢查询: 1.慢查询的作用:通过慢查询分析,找到有问题的命令进行优化. 2.慢查询的redis的配置参数: slowlog-log-slower-than 慢查询预设阈值(单位是微秒1秒=1000 ...

  2. 算法寒假实习面试经过之 十一贝(offer) 联想研究院(电话一面 被拒)

    联想研究院 1面 自我介绍 聊比赛,讲了讲jdd的. 感觉都快要背过了... 之前重复的问题就不说了,说一下印象比较深的 adaboost 与gbdt的区别 随机森林,如果有t个特征,n个树,每个树深 ...

  3. ZOJ 3959 Problem Preparation 【水】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 AC代码 #include <cstdio> ...

  4. PAT 天梯赛 L1-020. 帅到没朋友 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L1-020 思路 对于每个 K >= 2 的朋友圈,里面的所有 ID 都用 MAP 标记一下 对于每个 K = ...

  5. Linux Shell基础 通配符

    通配符 在 Bash 中,如果需要模糊匹配文件名或目录名,就要用到通配符.下面为常用的通配符. 表 1 通配符 通配符 作 用 ? 匹配一个任意字符 * 匹配 0 个或任意多个任意字符,也就是可以匹配 ...

  6. rsh命令配置于使用

    安装环境:一台centos6.10虚拟机,一台centos7.5虚拟机,全部使用root用户登录. 两台机器上都要安装rsh.rsh-server.xinetd包. 两台机器都要关闭防火墙并配置/et ...

  7. CSS3手风琴下拉菜单

    在线演示 本地下载

  8. samtools+bcftools 进行SNP calling

    两个软件的作用:1.samtools mpileup 主要是用于收集BAM文件中的信息,这个位点上有多少条read匹配,匹配read的碱基是什么,并将这些信息存储在BCF文件中.2.bcftools ...

  9. INSPIRED启示录 读书笔记 - 第29章 大公司如何创新

    大公司实现创新的方法 20%法则:谷歌的程序员有20%的工作时间可以用来从事创新研究,这个方法最早是从施乐帕克研究所学来的.20%法则鼓励普通员工自己尝试各种想法,让员工打心底愿意倾注更多的激情和汗水 ...

  10. java filter 实现权限控制

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...