给定一个长度为N的二进制字符串str ,任务是通过将二进制字符串拆分为两个非空子字符串来找到左子字符串上 0 的计数和右子字符串上 1 的计数可能的最大总和。
例子:
Input: str = “000111”
Output: 6
Explanation:
Splitting the binary string into “000” and “111”.
Count of 0s in the left substring of the string = 3
Count of 1s in the right substring of the string = 3
Therefore, the sum of the count of 0s in the left substring of the string and the count of 1s in the right substring of the string = 3 + 3 = 6.
Input: S = “1111”
Output: 3
Explanation:
Splitting the binary string into “1” and “111”.
Count of 0s in the left substring of the string = 0
Count of 1s in the right substring of the string = 3
Therefore, the sum of the count of 0s in the left substring of the string and the count of 1s in the right substring of the string = 0 + 3 = 3.
处理方法:按照以下步骤解决问题:
- 初始化一个变量,例如res ,以存储左子串中0的计数和右子串中1的计数的最大总和。
- 初始化一个变量,比如cntOne ,以在给定的二进制字符串存储1的计数。
- 遍历二进制字符串,对于每个字符,检查它是否为“1” 。如果发现为真,则将cntOne的值增加1 。
- 初始化两个变量,比如0和1 ,以存储0 s 的计数和1 s 的计数,直到第i个索引。
- 遍历二进制字符串并更新res = max(res, cntOne – one + zero) 的值。
- 最后,打印res的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the maximum sum of count of
// 0s in the left substring and count of 1s in
// the right substring by splitting the string
int maxSumbySplittingstring(string str, int N)
{
// Stores count of 1s
// the in binary string
int cntOne = 0;
// Traverse the binary string
for (int i = 0; i < N; i++) {
// If current character is '1'
if (str[i] == '1') {
// Update cntOne
cntOne++;
}
}
// Stores count of 0s
int zero = 0;
// Stores count of 1s
int one = 0;
// Stores maximum sum of count of
// 0s and 1s by splitting the string
int res = 0;
// Traverse the binary string
for (int i = 0; i < N - 1; i++) {
// If current character
// is '0'
if (str[i] == '0') {
// Update zero
zero++;
}
// If current character is '1'
else {
// Update one
one++;
}
// Update res
res = max(res, zero + cntOne - one);
}
return res;
}
// Driver Code
int main()
{
string str = "00111";
int N = str.length();
cout << maxSumbySplittingstring(str, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the maximum sum of count of
// 0s in the left subString and count of 1s in
// the right subString by splitting the String
static int maxSumbySplittingString(String str, int N)
{
// Stores count of 1s
// the in binary String
int cntOne = 0;
// Traverse the binary String
for (int i = 0; i < N; i++)
{
// If current character is '1'
if (str.charAt(i) == '1')
{
// Update cntOne
cntOne++;
}
}
// Stores count of 0s
int zero = 0;
// Stores count of 1s
int one = 0;
// Stores maximum sum of count of
// 0s and 1s by splitting the String
int res = 0;
// Traverse the binary String
for (int i = 0; i < N - 1; i++) {
// If current character
// is '0'
if (str.charAt(i) == '0') {
// Update zero
zero++;
}
// If current character is '1'
else {
// Update one
one++;
}
// Update res
res = Math.max(res, zero + cntOne - one);
}
return res;
}
// Driver Code
public static void main(String[] args)
{
String str = "00111";
int N = str.length();
System.out.print(maxSumbySplittingString(str, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to find the maximum sum of count of
# 0s in the left suband count of 1s in
# the right subby splitting the string
def maxSumbySplittingstring(str, N):
# Stores count of 1s
# the in binary string
cntOne = 0
# Traverse the binary string
for i in range(N):
# If current character is '1'
if (str[i] == '1'):
# Update cntOne
cntOne += 1
# Stores count of 0s
zero = 0
# Stores count of 1s
one = 0
# Stores maximum sum of count of
# 0s and 1s by splitting the string
res = 0
# Traverse the binary string
for i in range(N - 1):
# If current character
# is '0'
if (str[i] == '0'):
# Update zero
zero += 1
# If current character is '1'
else:
# Update one
one += 1
# Update res
res = max(res, zero + cntOne - one)
return res
# Driver Code
if __name__ == '__main__':
str = "00111"
N = len(str)
print (maxSumbySplittingstring(str, N))
# This code is contributed by mohit kumar 29.
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to find the maximum sum of count of
// 0s in the left subString and count of 1s in
// the right subString by splitting the String
static int maxSumbySplittingString(String str, int N)
{
// Stores count of 1s
// the in binary String
int cntOne = 0;
// Traverse the binary String
for (int i = 0; i < N; i++)
{
// If current character is '1'
if (str[i] == '1')
{
// Update cntOne
cntOne++;
}
}
// Stores count of 0s
int zero = 0;
// Stores count of 1s
int one = 0;
// Stores maximum sum of count of
// 0s and 1s by splitting the String
int res = 0;
// Traverse the binary String
for (int i = 0; i < N - 1; i++) {
// If current character
// is '0'
if (str[i] == '0') {
// Update zero
zero++;
}
// If current character is '1'
else {
// Update one
one++;
}
// Update res
res = Math.Max(res, zero + cntOne - one);
}
return res;
}
// Driver Code
public static void Main(String[] args)
{
String str = "00111";
int N = str.Length;
Console.Write(maxSumbySplittingString(str, N));
}
}
// This code is contributed by 29AjayKumar
Javascript
5
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live