📌  相关文章
📜  通过拆分给定的二进制字符串最大化左子串中的 0 和右子串中的 1 的计数

📅  最后修改于: 2021-09-06 05:46:32             🧑  作者: Mango

给定二进制字符串str ,任务是通过在任何索引处拆分给定的二进制字符串来最大化左子字符串中 0 和右子字符串中 1 的计数。最后打印这些 0 和 1 的总和。

例子:

方法:

  1. 计算给定二进制字符串str 中 1 的数量(比如totalOnes )。
  2. 遍历给定的字符串并保留 0(比如)和 1(比如)的计数。
  3. 在字符串遍历期间,将最大和更新为:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to maximize the sum of the count
// of zeros and ones in the left and right
// substring
int maxSum(string& str)
{
 
    int maximumSum = 0;
 
    // To store the total ones
    int totalOnes;
 
    // Count the total numbers of ones
    // in string str
    totalOnes = count(str.begin(),
                      str.end(), '1');
 
    // To store the count of zeros and
    // ones while traversing string
    int zero = 0, ones = 0;
 
    // Interate the given string and
    // update the maximum sum
    for (int i = 0; str[i]; i++) {
 
        if (str[i] == '0') {
            zero++;
        }
        else {
            ones++;
        }
 
        // Update the maximum Sum
        maximumSum
            = max(
                maximumSum,
                zero + (totalOnes - ones));
    }
 
    return maximumSum;
}
 
// Driver Code
int main()
{
    // Given binary string
    string str = "011101";
 
    // Function call
    cout << maxSum(str);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
// Function to maximize the sum 
// of the count of zeros and ones 
// in the left and right substring
static int maxSum(String str)
{
    int maximumSum = 0;
 
    // To store the total ones
    int totalOnes = 0;
 
    // Count the total numbers of ones
    // in string str
    for(int i = 0; i < str.length(); i++)
    {
       if (str.charAt(i) == '1')
       {
           totalOnes++;
       }
    }
     
    // To store the count of zeros and
    // ones while traversing string
    int zero = 0, ones = 0;
 
    // Interate the given string and
    // update the maximum sum
    for(int i = 0; i < str.length(); i++)
    {
       if (str.charAt(i) == '0')
       {
           zero++;
       }
       else
       {
           ones++;
       }
        
       // Update the maximum Sum
       maximumSum = Math.max(maximumSum,
                            zero + (totalOnes - ones));
    }
     
    return maximumSum;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given binary string
    String str = "011101";
 
    // Function call
    System.out.println(maxSum(str));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
 
# Function to maximize the sum of the count
# of zeros and ones in the left and right
# substring
def maxSum(str):
 
    maximumSum = 0
 
    # To store the total ones
 
    # Count the total numbers of ones
    # in str
    totalOnes = 0
    for i in str:
        if i == '1':
            totalOnes += 1
 
    # To store the count of zeros and
    # ones while traversing string
    zero = 0
    ones = 0
 
    # Interate the given and
    # update the maximum sum
    i = 0
    while i < len(str):
 
        if (str[i] == '0'):
            zero += 1
        else:
            ones += 1
 
        # Update the maximum Sum
        maximumSum= max(maximumSum,zero + (totalOnes - ones))
        i += 1
 
    return maximumSum
 
# Driver Code
if __name__ == '__main__':
    # Given binary string
    str = "011101"
 
    # Function call
    print(maxSum(str))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to maximize the sum
// of the count of zeros and ones
// in the left and right substring
static int maxSum(string str)
{
    int maximumSum = 0;
 
    // To store the total ones
    int totalOnes = 0;
 
    // Count the total numbers of ones
    // in string str
    for(int i = 0; i < str.Length; i++)
    {
       if (str[i] == '1')
       {
           totalOnes++;
       }
    }
     
    // To store the count of zeros and
    // ones while traversing string
    int zero = 0, ones = 0;
 
    // Interate the given string and
    // update the maximum sum
    for(int i = 0; i < str.Length; i++)
    {
       if (str[i] == '0')
       {
           zero++;
       }
       else
       {
           ones++;
       }
        
       // Update the maximum Sum
       maximumSum = Math.Max(maximumSum, zero +
                                   (totalOnes -
                                    ones));
    }
    return maximumSum;
}
 
// Driver Code
public static void Main(string []args)
{
     
    // Given binary string
    string str = "011101";
 
    // Function call
    Console.Write(maxSum(str));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
5

时间复杂度: O(N) ,其中 N 是字符串的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live