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.
class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
ans=0 for i in range(len(s)-1):
if s[i]!=s[i+1]:
ans+=self.findBS(i,s) return ans def findBS(self,i,s):
count=1
while i-count>=0 and i+1+count<len(s) and s[i-count]==s[i] and s[i+1]==s[i+1+count]:
count+=1
return count

  

 

[LeetCode&Python] Problem 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 解题报告(Python)

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

  4. LeetCode 696 Count Binary Substrings 解题报告

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

  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

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

  7. [LeetCode&Python] Problem 226. Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  8. 696. Count Binary Substrings统计配对的01个数

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

  9. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

随机推荐

  1. Jackson 工具类使用及配置指南

    目录 前言 Jackson使用工具类 Jackson配置属性 Jackson解析JSON数据 Jackson序列化Java对象 前言 Json数据格式这两年发展的很快,其声称相对XML格式有很对好处: ...

  2. svn分支使用 SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤

    比较好的介绍了分支的创建和使用. 转:http://blog.csdn.net/vbirdbest/article/details/51122637

  3. 原 spring-boot工程中,jpa下hibernate的ddl-auto的各种属性

    jpa:  hibernate:    ddl-auto: create ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空 ddl-auto:create-d ...

  4. Ubuntu16.10下mysql5.7的安装及远程访问配置

    如何安装mysql 1.sudo apt-get update,如果很慢或者失败,需要在软件和更新中选择最佳服务器,勾选所有互联网下载选项及去掉其他软件所有勾选项 2.sudo apt-get upg ...

  5. 【Loadrunner_Http接口】使用Loadrunner对天气信息的接口编写脚本

    方法一:使用get请求 Action() { //http接口访问,get请求 web_url("www.abc.com", "URL=http://v.juhe.cn/ ...

  6. sql2008r2安装失败的解决办法

    setup fails with: '.', hexadecimal value 0x00, is an invalid character.SQL 2012 Setup issues - hexad ...

  7. Javascript this 的一些总结

    1.1.1 摘要 相信有C/C++.C#或Java等编程经验的各位,对于this关键字再熟悉不过了.由于Javascript是一种面向对象的编程语言,它和C/C++.C#或Java一样都包含this关 ...

  8. day16 初识面向对象

    今天主要学习内容: 1.初始面向对象 2 .类 ,对象 3,面向对象和面向过程的对比 4.面向对象的三大特征 1,初始面向对象 面向过程 : 一切以实物的发展流程为中心 面向对象: 一切以对象为中心, ...

  9. swiftlint 你所要知道的所有!!

    swiftin Should the opening brace of a function or control flow statement be on a new line or not ?:) ...

  10. Xilinx 7 series FPGA multiboot技术的使用(转)

    reference:https://www.cnblogs.com/chensimin1990/p/9067629.html 当升级程序有错误的时候,系统会启动golden bitstream 注意: ...