原题链接在这里: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. junit在idea中的使用(1)--理论篇

     感觉本文前部分配置太过繁琐,大家可以参考我的这篇文章http://www.cnblogs.com/SuMeng/p/8279879.html(junit在IDEA中使用--实践篇),用添加maven ...

  2. OS X 与传统Unix的一点区别

    在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...

  3. 每天一个Linux命令(47)route命令

        Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table).     (1)用法:     用法:  route ...

  4. hadoop05---进程线程

    J2ee是一种规范,tomcat.jboss.weblogic就是实现.JMS是一种规范,ActiveMQ是实现. .1.1. 进程介绍.线程介绍 进程:它是内存中的一段独立的内存空间. 线程:是在进 ...

  5. 【HackerRank】Sherlock and Array

    Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in ...

  6. Struts2笔记02——Struts2 概述(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Struts2是基于MVC设计模式的一种流行.成熟的We ...

  7. 主攻ASP.NET MVC4.0之重生:CheckBoxListHelper和RadioBoxListHelper的使用

    在项目中新建Helpers文件夹,创建CheckBoxListHelper和RadioBoxListHelper类. CheckBoxListHelper代码 using System; using ...

  8. easy_install

    一 setuptools 和easy_install setuptools:setuptools 是一组由PEAK(Python Enterprise Application Kit)开发的 Pyth ...

  9. async/await方法解析

    欲了解await,必须先了解Promise,可参考: http://www.cnblogs.com/yanze/p/6347646.html 支持度: ES6已支持Promise,ES7也决定支持aw ...

  10. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...