本文概述
- C ++
- Java
- Python3
- C#
例子:
【将二进制字符串拆分为0和1相等数量的子字符串】输入:str =" 0100110101"方法:初始化计数= 0并逐个字符地遍历字符串, 并跟踪0s和1s到目前为止, 每当0s和1s变得相等, 增加计数。如果算0s和1s原字符串中的不等于则打印-1否则, 在遍历整个字符串之后打印count的值。
输出:4
所需的子字符串为" 01", " 0011", " 01"和" 01"。
输入:str =" 0111100010"
输出:3
下面是上述方法的实现:
C ++
//C++ implementation of the approach
#include <
bits/stdc++.h>
using namespace std;
//Function to return the count
//of maximum substrings str
//can be divided into
int maxSubStr(string str, int n)
{//To store the count of 0s and 1s
int count0 = 0, count1 = 0;
//To store the count of maximum
//substrings str can be divided into
int cnt = 0;
for ( int i = 0;
i <
n;
i++) {
if (str[i] == '0' ) {
count0++;
}
else {
count1++;
}
if (count0 == count1) {
cnt++;
}
}//It is not possible to
//split the string
if (count0 != count1) {
return -1;
}return cnt;
}//Driver code
int main()
{
string str = "0100110101" ;
int n = str.length();
cout <
<
maxSubStr(str, n);
return 0;
}
Java
//Java implementation of the above approach
class GFG
{//Function to return the count
//of maximum substrings str
//can be divided into
static int maxSubStr(String str, int n)
{//To store the count of 0s and 1s
int count0 = 0 , count1 = 0 ;
//To store the count of maximum
//substrings str can be divided into
int cnt = 0 ;
for ( int i = 0 ;
i <
n;
i++)
{
if (str.charAt(i) == '0' )
{
count0++;
}
else
{
count1++;
}
if (count0 == count1)
{
cnt++;
}
}//It is not possible to
//split the string
if (count0 != count1)
{
return - 1 ;
}
return cnt;
}//Driver code
public static void main(String []args)
{
String str = "0100110101" ;
int n = str.length();
System.out.println(maxSubStr(str, n));
}
}//This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach# Function to return the count
# of maximum substrings str
# can be divided into
def maxSubStr( str , n):# To store the count of 0s and 1s
count0 = 0
count1 = 0# To store the count of maximum
# substrings str can be divided into
cnt = 0for i in range (n):
if str [i] = = '0' :
count0 + = 1
else :
count1 + = 1if count0 = = count1:
cnt + = 1# It is not possible to
# split the string
if count0 ! = count1:
return - 1return cnt# Driver code
str = "0100110101"
n = len ( str )
print (maxSubStr( str , n))
C#
//C# implementation of the above approach
using System;
class GFG
{//Function to return the count
//of maximum substrings str
//can be divided into
static int maxSubStr(String str, int n)
{//To store the count of 0s and 1s
int count0 = 0, count1 = 0;
//To store the count of maximum
//substrings str can be divided into
int cnt = 0;
for ( int i = 0;
i <
n;
i++)
{
if (str[i] == '0' )
{
count0++;
}
else
{
count1++;
}
if (count0 == count1)
{
cnt++;
}
}//It is not possible to
//split the string
if (count0 != count1)
{
return -1;
}
return cnt;
}//Driver code
public static void Main(String []args)
{
String str = "0100110101" ;
int n = str.Length;
Console.WriteLine(maxSubStr(str, n));
}
}//This code is contributed by PrinciRaj1992
输出如下:
4
时间复杂度:O(N)其中N是字符串的长度
空间复杂度:O(1)
推荐阅读
- 从BST构建二叉树,使其遍历级别顺序可打印排序的数据
- win10变回win7图文详细教程图解
- 本图文详细教程教你各版本Ghost windows10系统激活码密钥
- 最新通用版win10激活密钥分享制作详细说明
- win10 64位 产品密钥分享制作详细说明
- windows10专业版激活密钥分享制作详细说明
- 系统之家ghost win10系统最新推荐
- 本图文详细教程教你如何处理Ghost windows10系统屏幕一直
- 本图文详细教程教你windows7系统升级到win10 64位