Given two binary strings str1 and str2 each of length N, the task is to split the strings in such a way that the sum is maximum with given conditions.
- Split both strings at the same position into equal length substrings.
- If both the substrings have only 0’s then the value of that substring to be added is 1.
- If both the substrings have only 1’s then the value of that substring to be added is 0.
- If both the substrings have 0’s and 1’s then the value of that substring to be added is 2.
Return the maximum sum possible.
Examples:
Input: str1 = “0101000”, str2 = “1101100”
Output: 8
Explanation:
Split like this:
Take “0” from str1 and “1” from str2 -> 2
Take “10” from str1 and “10” from str2 -> 2
Take “1” from str1 and “1” from str2 -> 0
Take “0” from str1 and “1” from str2 -> 2
Take “0” from str1 and “0” from str2 -> 1
Take “0” from str1 and “0” from str2 -> 1
Sum is 2 + 2 + 0 + 2 + 1 + 1 => 8
Input: str1 = “01”, str2 = “01”
Output: 2
Approach: This problem can be solved using the greedy approach. First, take some of the distinct pair means (0, 1) because it gives the maximum value 2 or with a distinct value pair with the same value pair, the maximum value is 2 so there is no benefit. then try to make pair of (0, 0) with (1, 1) or vice versa to maximize the sum.
- Initialize the MaxSum to 0.
- Traverse the strings. If Ai is not equal to Bi, increment the MaxSum by 2.
- Traverse the strings again and try to pair with opposite value means 0 with 1 or 1 with 0.
- Check previous and next value of string if it’s opposite increment MaxSum by 2.
- Else if the pair is only (0, 0) increment MaxSum by 1.
Below is the implementation of the above-mentioned approach:
C++
#include <iostream>
using namespace std;
int MaxSum(string a, string b)
{
int ans = 0;
for ( int i = 0; i < a.length(); i++) {
if (a[i] != b[i])
ans += 2;
}
int n = a.length();
for ( int i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0' ) {
if (i + 1 < n and a[i + 1] == '1'
and b[i + 1] == '1' ) {
ans += 2, i++;
}
else {
ans += 1;
}
}
else {
if (i + 1 < n and a[i + 1] == '0'
and b[i + 1] == '0' ) {
ans += 2, i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
int main()
{
string a = "0101000" ;
string b = "1101100" ;
cout << MaxSum(a, b);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int MaxSum(String a, String b) {
int ans = 0 ;
for ( int i = 0 ; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i))
ans += 2 ;
}
int n = a.length();
for ( int i = 0 ; i < n; i++) {
if (a.charAt(i) == b.charAt(i)) {
if (a.charAt(i) == '0' ) {
if (i + 1 < n && a.charAt(i + 1 ) == '1'
&& b.charAt(i + 1 ) == '1' ) {
ans += 2 ;
i++;
}
else {
ans += 1 ;
}
} else {
if (i + 1 < n && a.charAt(i + 1 ) == '0'
&& b.charAt(i + 1 ) == '0' ) {
ans += 2 ;
i++;
} else {
ans += 0 ;
}
}
}
}
return ans;
}
public static void main(String args[]) {
String a = "0101000" ;
String b = "1101100" ;
System.out.println(MaxSum(a, b));
}
}
|
Python3
def MaxSum(a, b):
ans = 0
for i in range ( 0 , len (a)):
if (a[i] ! = b[i]):
ans + = 2
n = len (a)
i = 0
while i < n:
if (a[i] = = b[i]):
if (a[i] = = '0' ):
if (i + 1 < n and a[i + 1 ] = = '1'
and b[i + 1 ] = = '1' ):
ans, i = ans + 2 , i + 1
else :
ans + = 1
else :
if (i + 1 < n and a[i + 1 ] = = '0'
and b[i + 1 ] = = '0' ):
ans, i = ans + 2 , i + 1
else :
ans + = 0
i + = 1
return ans
if __name__ = = "__main__" :
a = "0101000"
b = "1101100"
print (MaxSum(a, b))
|
C#
using System;
class GFG
{
static int MaxSum( string a, string b)
{
int ans = 0;
for ( int i = 0; i < a.Length; i++) {
if (a[i] != b[i])
ans += 2;
}
int n = a.Length;
for ( int i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0' ) {
if (i + 1 < n && a[i + 1] == '1'
&& b[i + 1] == '1' ) {
ans += 2;
i++;
}
else {
ans += 1;
}
}
else {
if (i + 1 < n && a[i + 1] == '0'
&& b[i + 1] == '0' ) {
ans += 2;
i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
public static void Main()
{
string a = "0101000" ;
string b = "1101100" ;
Console.Write(MaxSum(a, b));
}
}
|
Javascript
<script>
function MaxSum(a, b) {
let ans = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i])
ans += 2;
}
let n = a.length;
for (let i = 0; i < n; i++) {
if (a[i] == b[i]) {
if (a[i] == '0' ) {
if (i + 1 < n && a[i + 1] == '1'
&& b[i + 1] == '1' ) {
ans += 2, i++;
}
else {
ans += 1;
}
}
else {
if (i + 1 < n && a[i + 1] == '0'
&& b[i + 1] == '0' ) {
ans += 2, i++;
}
else {
ans += 0;
}
}
}
}
return ans;
}
let a = "0101000" ;
let b = "1101100" ;
document.write(MaxSum(a, b));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)