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. Ubuntu 下matlab 查看memory函数

    %Copyright (c) 2012, Michael Hirsch%All rights reserved.%%Redistribution and use in source and binar ...

  2. laravel管理员表中的模型

    <?php namespace App; use App\Model; use Illuminate\Foundation\Auth\User as Authenticatable; class ...

  3. python 自然语言处理(三)____条件频率分布

    条件频率分布就是频率分布的集合,每个频率分布有一个不同的“条件”,这个条件通常是文本的类别.当语料文本分为几类(文体,主题,作者等)时,可以计算每个类别独立的频率分布,这样,就可以通过条件频率分布研究 ...

  4. 用linux命令连接无线网络-转载

    首先是用到的工具: ifconfigrouteiwlistiwconfig 后两个是无线工具 从现在开始,按我的步骤做 (##后面的是说明部分) 1.开启无线,如果是笔记本,开启无线开关,或用Fn+F ...

  5. owin启动事项

    在上下文中找不到 owin.Environment 项 owin没有启动. 尝试加载应用时出现了以下错误.- 找不到包含 OwinStartupAttribute 的程序集 startup类不是通过v ...

  6. django+xadmin+djcelery实现后台管理定时任务

    继上一篇中间表的数据是动态的,图表展示的数据才比较准确.这里用到一个新的模块Djcelery,安装配置步骤如下: 1.安装 redis==2.10.6 celery==3.1.23 django-ce ...

  7. Java正则表达式校验

    package com.study.string; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达 ...

  8. oracle日志相关的表

    SELECT * FROM all_objects t where  object_name like '%EN_CONCAT_IM%';DBA_HIST_SQLTEXTDBA_HIST_SQLSTA ...

  9. SignalR 前期简单配置

    一.随便你在哪个命名空间下新建一个Startup类,并在在该类中注册SignalR. using Owin; using System; using System.Collections.Generi ...

  10. :迭代器模式1:Iterator

    //今天一口气把这一章前半部分的iterator例子的所有代码写完,涉及到了不少指针的内容,竟然一次性编译通过.... //Iterator与Menu之间应该不是has a的关系,先这样着吧. #if ...