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' 题意:给定一个由0和1组成的非空字符串,计算出由相同数量的0和1、且0和1分别连续的子串的个数。
思路1:直接暴力解决,但时间复杂度高,提交后time limit exceeded。从前往后遍历,判断当前数字的连续相同数字的数量,再找出从第一个不同数字开始,连续不同数字的数量,数量相同则count加1,否则count不变。
代码如下:
public int countBinarySubstrings(String s) {
char[] chars = s.toCharArray();
int count = 0;
for(int i = 0; i < chars.length - 1; i++){
if(isSubstrings(chars, i)){
count++;
}
}
return count;
}
public boolean isSubstrings(char[] chars, int start){
int same = start + 1;
while(same < chars.length - 1 && chars[start] == chars[same]){
same++;
}
int diff = same;
while(diff < chars.length && chars[diff] != chars[start] && diff - same < same - start){
diff++;
}
return diff - same == same - start ? true : false;
}

思路2:(LeetCode提供的算法1)在字符串s中,统计相同数字连续出现了多少次。例:s = "00110",则group = {2,2,1}。这样,在统计有多少个满足条件的子串时,对group数组从前往后遍历,依次取min{group[i], group[i+1]},再求和即可。

原因:以group[i]=2, group[i+1]=3为例,则表示“00111”或“11000”。
以“00111”为例,group={2,3},当第一个1出现时,前面已经有2个0了,所以肯定能组成01;再遇到下一个1时,此时有2个0,2个1,所以肯定能组成0011;再遇到下一个1时,前面只有2个0,而此时有3个1,所以不可以再组成满足条件的子串了。

代码如下:

public int contBinarySubstrings(String s){
char[] chars = s.toCharArray();
int[] group = new int[chars.length];
int index = 0;
group[0] = 1;
for(int i = 1; i < chars.length; i++){
if(chars[i] == chars[i - 1])
group[index]++;
else
group[++index] = 1;
}
int i = 0, count = 0;
while(i < group.length - 1 && group[i] != 0){
count += Math.min(group[i], group[i + 1]);
i++;
}
return count;
}

思路3:(LeetCode提供的算法2)定义两个变量pre和cur,pre存储当前数字前的数字连续次数,cur存储当前数字的连续次数。然后从第二个数字开始遍历,如果当前数字与前面数字相同,则cur自增1;否则对pre和cur取最小值,记为子串次数,并将pre赋值为cur,cur重置为1。原理与上一个方法相同,但没有定义数组,空间复杂度降低。

代码如下:


public int contBinarySubstring(String s){
int pre = 0, cur = 1, count = 0;
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) != s.charAt(i - 1)){
count += Math.min(pre, cur);
pre = cur;
cur = 1;
}
else{
cur++;
}
}
return count + Math.min(pre, cur);
}

 

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

  1. LeetCode 696 Count Binary Substrings 解题报告

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

  2. 696. Count Binary Substrings - LeetCode

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

  3. 【Leetcode_easy】696. Count Binary Substrings

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

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

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

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

  6. 696. Count Binary Substrings

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

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

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

  8. [LeetCode] 696. Count Binary Substrings_Easy

    利用group, 将每个连着的0或者1计数并且append进入group里面, 然后再将group里面的两两比较, 得到min, 并且加入到ans即可.   T: O(n)   S: O(n)  比较 ...

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

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

随机推荐

  1. 20155339 2016-2017 2 《Java程序设计》第2周学习总结

    20155339 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 这周学习了课本的第三章,主要内容是JAVA的基础语法,在这章的学习过程中我发现大部分与c语言 ...

  2. windows phone 手机解锁失败问题

    1.使用 VS 2015  自带的  Windows Phone Developer Registration (8.1) 工具, 解锁手机. 总是提示 日期和时间错误. 解决办法,  有2个 1.打 ...

  3. C#基础之委托

    委托常常和事件在一起使用,可以理解委托是方法的容器,事件则是委托的另一种表现形式.委托和事件虽然写得还比较多,不过也只是用的熟练而已,趁周末没课好好巩固下基础,一点一点积累吧. 1.一个简单的小例子 ...

  4. RHCSA day5

    4.调整逻辑卷容量 请按照以下要求调整本地逻辑卷lvm1的容量: 调整后的逻辑卷及文件系统大小为770MiB 调整后确保文件系统中已存在的内容不能被破坏 调整后的容量可能出现误差,只要在730MiB ...

  5. 【洛谷P4178】Tree

    题面 题解 感觉和\(CDQ\)分治一样套路啊 首先,构建出点分树 对于每一层分治重心,求出它到子树中任意点的距离 然后\(two-pointers\)计算满足小于等于\(K\)的点对数目,加入答案 ...

  6. SPOJ11469 SUBSET

    题面 Farmer John's owns N cows (2 <= N <= 20), where cow i produces M(i) units of milk each day ...

  7. iOS 中架构模式的浅显理解

    我们开发软件中应用各种模式,主要是为了 职责划分:一个类只做一件事 易用,可维护,方便扩展 解耦,相互独立,可单独测试 各种设计模式其实都是在解决上面的问题,让我们对比看看吧. 一.如何理解MVC设计 ...

  8. C#反射的简单示例

    反射(Reflection)可以在运行时获 得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的名称.限定符和参数等反正说白了就 ...

  9. python2 - 列表

    列表 a = [1,2,3,4,5,6,7] a[0:4:1]//正向索引 a[-1:-2:-1]//反向索引 列表添加 a = [1, 2] b = [3, 4] +:a + b//把a和b连接,重 ...

  10. MAC终端安装指定版本node

    MAC终端安装指定版本node 安装brew 终端上运行 $ /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Home ...