问题描述:

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.

思路:

由于s中只有0或1,并且要组成对必须是相邻的0和1才可以,基于这点,只考虑相邻的0、1即可

数量如何确定呢?明显有多少个1,就需要配置多少个0,故相邻01组合最终能凑成多少对,看0和1谁的个数最少即可。

代码:

 class Solution:
def countBinarySubstrings(self, s: str) -> int:
s = list(map(len, s.replace('', '1 0').replace('', '0 1').split()))
return sum(min(i, j) for i, j in zip(s, s[1:]))

第一行代码,将01的字符串分组,每组仅仅包含0或仅仅包含1。同时计算每组中元素的个数

第二行代码,zip将相邻两个分组组合起来(每个分组中存储的是该分组元素个数),每个组合中较小的数字代表 该组组合能够拆成的符合条件的子串个数,然后将所有组合能够拆成的子串个数相加,得到的结果即是最终所有的个数

Python3解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. LeetCode Count Binary Substrings

    原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...

  3. Python3解leetcode Count Primes

    问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...

  4. 696. Count Binary Substrings - LeetCode

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

  5. 【Leetcode_easy】696. Count Binary Substrings

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

  6. LeetCode 696. Count Binary Substrings

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

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

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

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

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

  9. LeetCode 696 Count Binary Substrings 解题报告

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

随机推荐

  1. 007-elasticsearch5.4.3【一】概述、Elasticsearch 访问方式、Elasticsearch 面向文档、常用概念

    一.概述 Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Elasticsearch 也是使用 Java 编写的,它的内部使用 L ...

  2. oracle系统调优

    在Oracle数据库系统中,起到调节作用的参数叫初始化参数,在Oracle 8i及以前的版本中,这些初始化参数记录在INITsid.ora文件中:而Oracle 9i/10g/11g中将这些参数记录在 ...

  3. 安卓手机作为中继器-连接Wifi共享该Wifi给PC和手机

    工具 routernet.apk

  4. [Python3] 007 列表的遍历,你是 for 联盟还是 while 部落

    目录 少废话,直接上例子 for 联盟 for 与 list 的简单合作 for 的老搭档 range() for 与嵌套列表(双层列表) for 从 if 那儿认识的 else 循环"三杰 ...

  5. [Python3 练习] 006 汉诺塔2 非递归解法

    题目:汉诺塔 II 接上一篇 [Python3 练习] 005 汉诺塔1 递归解法 这次不使用递归 不限定层数 (1) 解决方式 利用"二进制" (2) 具体说明 统一起见 我把左 ...

  6. Java学习day8面向对象编程2-类的属性和方法

    一.类的属性 1.语法格式 修饰符 类型 属性名 = 初值 说明:修饰符private:该属性只能由该类的方法使用.在同一类内可见.使用对象:变量.方法. 注意:不能修饰类(外部类)    修饰符pu ...

  7. HDU 4285 circuits( 插头dp , k回路 )

    circuits Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. python的cls,self,classmethod,staticmethod

    python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种, 一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问 ...

  9. OpenCV-----Numpy数组

    Nunmpy数组包含: 强大的N维数组对象 复杂的(广播)功能 集成C / C ++和Fortran代码的工具 有用的线性代数,傅立叶变换和随机数功能 遍历与修改数组中的所有像素点 #对所有像素进行循 ...

  10. Windows Server 搭建企业无线认证(NPS搭建)

    现代企业无线网络是必备,移动办公更是需求日益剧增.而带来的无线网络安全隐患随之而来,也是面临着巨大的挑战.所以对无线网络做接入认证是现在企业很迫切的需求. 上一遍已经说明了Radius认证方案:htt ...