Python3解leetcode Count Binary Substrings
问题描述:
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的更多相关文章
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
随机推荐
- Firefox,Chrome使用
Firefox 插件 REDIRECTOR Automatically redirect pages based on user-defined rules. 根据用户定义的规则自动重定向页面的插件. ...
- PHP执行外部程序
备份/恢复数据库 exec - 执行一个外部程序(在php文件所在目录进行执行) 很久以前写的,很多方法是项目中的直接复制粘体用不了,只能提供下思路. 用到执行外部程序的就这一句: exec(&quo ...
- python-笔记(六)模块操作以及常用模块简介
模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思是说把python代码写到里面,文件名就是模块的名称,例如:model.py model就是模块名称. 什么是包? ...
- free pascal 修改字符集,会导致 dos 不能显示 汉字。 处理方法如下
http://www.cnblogs.com/yjken/p/3917932.html 让windows系统的DOS窗口也可以显示utf8字符集 C:\Users\Administrator> ...
- Visual Studio关于项目迁移或拉取代码产生的dll黄色感叹号警告问题解决方案
今天换了台大电脑,准备好好爽一下, 就把笔记本上的项目拷贝到了台式机上, 但是我没有拷贝解决方案整个文件夹,因为其中项目太多了,我就把其中一个项目的文件夹直接拷贝到电脑上,然后就出现了下面的情况. 这 ...
- django amdin后台改成中文
- 【MM系列】SAP MM模块-委外采购订单 把Warning转换成Error信息提示
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-委外采购订单 把W ...
- [Markdown] 03 进阶语法 第一弹
目录 1. YMAL 题头 2. 缩写 3. 强调 4. 自定义 <div> 标签 5. <cite> 标签 5. <code> 与 <br> 标签 6 ...
- Win10不能远程其他远程计算机的解决办法
Win10不能远程其他远程计算机的解决办法 转自: https://blog.csdn.net/qq_38197830/article/details/69488236 首先打开控制面板——> ...
- Leading and Trailing LightOJ - 1282 题解
LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...