Maximize given function by selecting equal length substrings from given Binary Strings
Last Updated :
06 Nov, 2023
Given two binary strings s1 and s2. The task is to choose substring from s1 and s2 say sub1 and sub2 of equal length such that it maximizes the function:
fun(s1, s2) = len(sub1) / (2xor(sub1, sub2))
Examples:
Input: s1= “1101”, s2= “1110”
Output: 3
Explanation: Below are the substrings chosen from s1 and s2
Substring chosen from s1 -> “110”
Substring chosen from s2 -> “110”
Therefore, fun(s1, s2) = 3/ (2xor(110, 110)) = 3, which is maximum possible.
Input: s1= “1111”, s2= “1000”
Output: 1
Approach: In order to maximize the given function large substrings needed to be chosen with minimum XOR. To minimize the denominator, choose substrings in a way such that XOR of sub1 and sub2 is always 0 so that the denominator term will always be 1 (20). So for that, find the longest common substring from the two strings s1 and s2, and print its length that would be the required answer.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dp[1000][1000];
int lcs(string s, string k, int n, int m)
{
for ( int i = 0; i <= n; i++) {
for ( int j = 0; j <= m; j++) {
if (i == 0 or j == 0) {
dp[i][j] = 0;
}
else if (s[i - 1] == k[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
}
else {
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1]);
}
}
}
return dp[n][m];
}
int main()
{
string s1 = "1110" ;
string s2 = "1101" ;
cout << lcs(s1, s2,
s1.size(), s2.size());
return 0;
}
|
Java
class GFG{
static int dp[][] = new int [ 1000 ][ 1000 ];
static int lcs(String s, String k, int n, int m)
{
for ( int i = 0 ; i <= n; i++) {
for ( int j = 0 ; j <= m; j++) {
if (i == 0 || j == 0 ) {
dp[i][j] = 0 ;
}
else if (s.charAt(i - 1 ) == k.charAt(j - 1 )) {
dp[i][j] = 1 + dp[i - 1 ][j - 1 ];
}
else {
dp[i][j] = Math.max(dp[i - 1 ][j],
dp[i][j - 1 ]);
}
}
}
return dp[n][m];
}
public static void main(String [] args)
{
String s1 = "1110" ;
String s2 = "1101" ;
System.out.print(lcs(s1, s2,
s1.length(), s2.length()));
}
}
|
Python3
import numpy as np;
dp = np.zeros(( 1000 , 1000 ));
def lcs( s, k, n, m) :
for i in range (n + 1 ) :
for j in range (m + 1 ) :
if (i = = 0 or j = = 0 ) :
dp[i][j] = 0 ;
elif (s[i - 1 ] = = k[j - 1 ]) :
dp[i][j] = 1 + dp[i - 1 ][j - 1 ];
else :
dp[i][j] = max (dp[i - 1 ][j], dp[i][j - 1 ]);
return dp[n][m];
if __name__ = = "__main__" :
s1 = "1110" ;
s2 = "1101" ;
print (lcs(s1, s2, len (s1), len (s2)));
|
C#
using System;
public class GFG{
static int [,]dp = new int [1000,1000];
static int lcs( string s, string k, int n, int m)
{
for ( int i = 0; i <= n; i++) {
for ( int j = 0; j <= m; j++) {
if (i == 0 || j == 0) {
dp[i, j] = 0;
}
else if (s[i - 1] == k[j - 1]) {
dp[i, j] = 1 + dp[i - 1, j - 1];
}
else {
dp[i, j] = Math.Max(dp[i - 1, j],
dp[i, j - 1]);
}
}
}
return dp[n, m];
}
public static void Main( string [] args)
{
string s1 = "1110" ;
string s2 = "1101" ;
Console.Write(lcs(s1, s2, s1.Length, s2.Length));
}
}
|
Javascript
<script>
var dp = new Array(1000);
for ( var i = 0; i < 1000; i++) {
dp[i] = new Array(1000);
}
function lcs( s, k, n, m)
{
for ( var i = 0; i <= n; i++) {
for ( var j = 0; j <= m; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
}
else if (s[i - 1] == k[j - 1]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
}
else {
dp[i][j] = Math.max(dp[i - 1][j],
dp[i][j - 1]);
}
}
}
return dp[n][m];
}
var s1 = "1110" ;
var s2 = "1101" ;
document.write(lcs(s1, s2, s1.length, s2.length))
</script>
|
Time Complexity: O(N*M), where N is the size of s1 and M is the size of s2.
Auxiliary Space: O(N*M), where N is the size of s1 and M is the size of s2.
Approach2: Using memoised version of dynamic programming
In order to maximize the given function large substrings needed to be chosen with minimum XOR. To minimize the denominator, choose substrings in a way such that XOR of sub1 and sub2 is always 0 so that the denominator term will always be 1 (20). So for that, find the longest common substring from the two strings s1 and s2, and print its length that would be the required answer.
To find longest common substring we will go with memoisation approach.
Algorithm:
- Take two strings as input: s1 and s2.
- Initialize a 2D array dp of size n+1 by m+1 with -1, where n is the length of s1 and m is the length of s2.
- Define a function lcs(s1, s2, n, m) that takes s1, s2, n, and m as input.
- If n or m is equal to 0, return 0 as the base case.
- If dp[n][m] is not equal to -1, return dp[n][m].
- If the last characters of s1 and s2 match, then return 1 + lcs(s1, s2, n-1, m-1).
- Otherwise, return the maximum of lcs(s1, s2, n-1, m) and lcs(s1, s2, n, m-1).
- In the main function, call lcs(s1, s2, n, m) and print the result.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dp[1000][1000];
int lcs(string s, string k, int n, int m)
{
if (n == 0 or m == 0) {
return 0;
}
if (dp[n][m] != -1)
return dp[n][m];
if (s[n - 1] == k[m - 1]) {
return dp[n][m] = 1 + lcs(s, k, n - 1, m - 1);
}
return dp[n][m] = max(lcs(s, k, n - 1, m),
lcs(s, k, n, m - 1));
}
int main()
{
string s1 = "1110" ;
string s2 = "1101" ;
memset (dp, -1, sizeof (dp));
cout << lcs(s1, s2,
s1.size(), s2.size());
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int [][] dp;
static int lcs(String s, String k, int n, int m) {
if (n == 0 || m == 0 ) {
return 0 ;
}
if (dp[n][m] != - 1 ) {
return dp[n][m];
}
if (s.charAt(n - 1 ) == k.charAt(m - 1 )) {
return dp[n][m] = 1 + lcs(s, k, n - 1 , m - 1 );
}
return dp[n][m] = Math.max(lcs(s, k, n - 1 , m), lcs(s, k, n, m - 1 ));
}
public static void main(String[] args) {
String s1 = "1110" ;
String s2 = "1101" ;
dp = new int [s1.length() + 1 ][s2.length() + 1 ];
for ( int [] row : dp) {
Arrays.fill(row, - 1 );
}
System.out.println(lcs(s1, s2, s1.length(), s2.length()));
}
}
|
Python3
def lcs(s, k, n, m):
if n = = 0 or m = = 0 :
return 0
if dp[n][m] ! = - 1 :
return dp[n][m]
if s[n - 1 ] = = k[m - 1 ]:
dp[n][m] = 1 + lcs(s, k, n - 1 , m - 1 )
return dp[n][m]
dp[n][m] = max (lcs(s, k, n - 1 , m), lcs(s, k, n, m - 1 ))
return dp[n][m]
s1 = "1110"
s2 = "1101"
dp = [[ - 1 for _ in range ( len (s2) + 1 )] for _ in range ( len (s1) + 1 )]
print (lcs(s1, s2, len (s1), len (s2)))
|
C#
using System;
public class GFG
{
static int [,] dp;
public static int LCS( string s, string k, int n, int m)
{
if (n == 0 || m == 0)
{
return 0;
}
if (dp[n, m] != -1)
return dp[n, m];
if (s[n - 1] == k[m - 1])
{
return dp[n, m] = 1 + LCS(s, k, n - 1, m - 1);
}
return dp[n, m] = Math.Max(LCS(s, k, n - 1, m), LCS(s, k, n, m - 1));
}
public static void Main( string [] args)
{
string s1 = "1110" ;
string s2 = "1101" ;
dp = new int [s1.Length + 1, s2.Length + 1];
for ( int i = 0; i <= s1.Length; i++)
{
for ( int j = 0; j <= s2.Length; j++)
{
dp[i, j] = -1;
}
}
Console.WriteLine(LCS(s1, s2, s1.Length, s2.Length));
}
}
|
Javascript
function lcs(s, k, n, m) {
const dp = new Array(n + 1).fill().map(() => new Array(m + 1).fill(-1));
if (n === 0 || m === 0) {
return 0;
}
if (dp[n][m] !== -1) {
return dp[n][m];
}
if (s[n - 1] === k[m - 1]) {
return dp[n][m] = 1 + lcs(s, k, n - 1, m - 1);
}
return dp[n][m] = Math.max(lcs(s, k, n - 1, m),
lcs(s, k, n, m - 1));
}
function main() {
const s1 = "1110" ;
const s2 = "1101" ;
const n = s1.length;
const m = s2.length;
console.log(lcs(s1, s2, n, m));
}
main();
|
Time Complexity: O(N*M), where N is the size of s1 and M is the size of s2.
Auxiliary Space: O(N*M), where N is the size of s1 and M is the size of s2.
Similar Reads
Maximize sum by splitting given binary strings based on given conditions
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 adde
7 min read
Maximize count of 0s in left and 1s in right substring by splitting given Binary string
Given a binary string str, the task is to split the given binary string at any index into two non-empty substrings such that the sum of counts of 0s in the left substring and 1s in the right substring is maximum. Print the sum of such 0s and 1s in the end. Examples: Input: str = "0011110011" Output:
6 min read
Minimize a binary string by repeatedly removing even length substrings of same characters
Given a binary string str of size N, the task is to minimize the length of given binary string by removing even length substrings consisting of sam characters, i.e. either 0s or 1s only, from the string any number of times. Finally, print the modified string. Examples: Input: str ="101001"Output: "1
8 min read
Maximum length of consecutive 1's in a binary string in Python using Map function
We are given a binary string containing 1's and 0's. Find the maximum length of consecutive 1's in it. Examples: Input : str = '11000111101010111' Output : 4 We have an existing solution for this problem please refer to Maximum consecutive oneâs (or zeros) in a binary array link. We can solve this p
1 min read
Substring of length K having maximum frequency in the given string
Given a string str, the task is to find the substring of length K which occurs the maximum number of times. If more than one string occurs maximum number of times, then print the lexicographically smallest substring. Examples: Input: str = "bbbbbaaaaabbabababa", K = 5Output: ababaExplanation:The sub
14 min read
Find an N-length Binary String having maximum sum of elements from given ranges
Given an array of pairs ranges[] of size M and an integer N, the task is to find a binary string of length N such that the sum of elements of the string from the given ranges is maximum possible. Examples: Input: N = 5, M = 3, ranges[] = {{1, 3}, {2, 4}, {2, 5}}Output: 01100Explanation:Range [1, 3]:
5 min read
Minimize hamming distance in Binary String by setting only one K size substring bits
Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are '0'. The task is to find the minimum Hamming distance after choosing a substring of size K and making all elements of string T as '1' only once. Examples: Input: S = "101", K = 2Output: 1Explana
7 min read
Maximize length of the String by concatenating characters from an Array of Strings
Find the largest possible string of distinct characters formed using a combination of given strings. Any given string has to be chosen completely or not to be chosen at all. Examples: Input: strings ="abcd", "efgh", "efgh" Output: 8Explanation: All possible combinations are {"", "abcd", "efgh", "abc
12 min read
Maximize subarrays of 0s of length X in given Binary String after flipping at most one '1'
Given a binary string str of length N and a positive integer X, the task is to maximize the count of X length subarrays consisting of only 0's by flipping at most one 1. One bit will be considered in only one subarray.Example: Input: str = "0010001", X = 2Output: 3Explanation: Flip str[2] from 1 to
10 min read
Maximize minority character deletions that can be done from given Binary String substring
Python Given binary string str of size, N. Select any substring from the string and remove all the occurrences of the minority character (i.e. the character having less frequency) from the substring. The task is to find out the maximum number of characters that can be removed from performing one suc
5 min read