给定长度为N的二进制字符串str ,任务是找到最大数量的子字符串str,可以将其划分为所有子字符串都平衡的状态,即它们具有相等的0和1数。如果无法拆分满足条件的str ,则打印-1 。
例子:
Input: str = “0100110101”
Output: 4
The required substrings are “01”, “0011”, “01” and “01”.
Input: str = “0111100010”
Output: 3
方法:初始化计数= 0和字符遍历字符串的字符和跟踪0,至今1的个数,只要0和1的数量相等增量计数。如果原始字符串的0和1s计数不相等,则打印-1,否则在遍历整个字符串之后打印count的值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 = 0
for i in range(n):
if str[i] == '0':
count0 += 1
else:
count1 += 1
if count0 == count1:
cnt += 1
# It is not possible to
# split the string
if count0 != count1:
return -1
return 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)