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.

Approach #1: String. [Java]

class Solution {
public int countBinarySubstrings(String s) {
int l = s.length();
int ret = 0;
int preRunLength = 0;
int curRunLength = 1;
char[] ch = s.toCharArray();
for (int i = 1; i < l; ++i) {
if (ch[i] == ch[i-1]) curRunLength++;
else {
preRunLength = curRunLength;
curRunLength = 1;
}
if (preRunLength >= curRunLength) ret++;
}
return ret;
}
}

  

696. Count Binary Substrings的更多相关文章

  1. 【Leetcode_easy】696. Count Binary Substrings

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

  2. 696. Count Binary Substrings - LeetCode

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

  3. LeetCode 696 Count Binary Substrings 解题报告

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

  4. [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 ...

  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. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

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

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

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

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

  9. [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings

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

随机推荐

  1. ElasicSearch(1)

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  2. C++ 日志生成 DLL

    示例: #define log_dbg(format,args...) \        printf("[DBG] [%s: %s() line:%d]: "format ,__ ...

  3. Linux安装配置JDK1.7

    1  在/usr/local   文件夹下新建一个文件夹software ,将JDK放到此文件夹中 并在此文件夹下解压执行命令  tar  zxvf  jdk-8u144-linux-x64.tar. ...

  4. [INet] WebSocket 数据收发的详细过程

    WebSocket 和 HTTP 相似,只是一个应用层协议,对下层透明,所以不涉及 TCP/IP. 由于浏览器支持了 WebSocket,所以在用 JS 写客户端的时候,是无需考虑数据的编码解码的. ...

  5. Match-----Correlation-----find_ncc_model_exposure

    * This example program shows how to use HALCON's correlation-based* matching. In particular it demon ...

  6. ABAP 省市县级联搜索帮助

    在展示ABAP代码之前,先建立自建表ZCHENH006,表中包含两个关键字段 BELNR(地区编码),SDESC(地区描述). 编码规则参考:身份证前六位地区编码规则,可参考我另外一篇Blog导入系统 ...

  7. Kb和KB的区别

  8. WIN10在loadrunner安装或path4插件安装时遇到的管理员阻止程序运行---解决方案(可用)

    方法1:关闭用户账户控制1.“win+x”进入控制面板,选择安全性与维护,在左侧更改windows smartscreen筛选器设置,选择"不执行任何操作",单击确定即可.2.“w ...

  9. php redis pipeline用法

    $redis_110 = Redis::connection('redis_110');$redis_85 = Redis::connection('redis_85');$zadd_key_arr ...

  10. 点击导出table表格

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...