Maximize given function by selecting equal length substrings from given Binary Strings


Two binary strings str1 and str2 of the same length are given and we have to maximize a given function value by choosing the substrings from the given strings of equal length. The given function is such that −

fun(str1, str2) = (len(substring))/(2^xor(sub1, sub2)).

Here, len(substring) is the length of the first substring while xor(sub1, sub2) is the xor of the given substrings as they are binary strings so it is possible.

Sample Examples

Input1: string str1 = 10110 & string str2 = 11101 
Output: 3

Explanation

We can choose a lot of different sets of strings to find the solution but choosing the ‘101’ from both strings will make the xor zero and that will result in the maximum value return from the function.

Input2: string str1 = 11111, string str2 = 10001 
Output: 1 

Explanation

We can choose ‘1’ as the substring and that will lead to this output and if we choose any other string that will produce the lower value.

Naive Approach

In this approach, we are going to find all the substrings and then we will compare them to find the solution but this solution is not efficient and will take a lot of time and space complexity.

To generate a substring of length x average time complexity is N^2 and then comparing each of the substring will cost N^2 more. Also, we have to find the xor of the given substrings that will also cost an extra factor of N which means N^5 will be the time complexity of the above code, which is highly inefficient.

Efficient Approach

Idea

The idea here comes with the simple observation that as the xor values get higher it will always reduce the answer. So, to maximize the function return value we have to reduce the xor value as less as possible.

The minimum XOR value that can be achieved is zero in the case where both the substrings are zero. So, this problem is actually derived from the problem: longest common substring.

When the XOR is zero then the dividend part is 1, so the final answer will be the length of the largest common substring.

Implementation

We have seen the idea to solve the problem and let us see the steps to implement the code −

  • We will create a function that will take two given strings as the input and return the integer value that will be our final result.

  • In the function, first, we will get the length of the strings and then we will create a 2-D vector of the size of the given strings multiplication.

  • We will use the nested for loops to traverse over the strings and get the largest common substring.

  • At each iteration, we will check if the current index of both the strings matches then we will get the value from the vector from the last index of both strings.

  • Otherwise, we will just make zero for the current index of the vector.

  • Also, we will maintain a variable to maintain the count of the maximum length of the common substring.

  • At last, we will return the answer and will print it in the main function.

Example

#include <bits/stdc++.h>
using namespace std;
// function to get the result
int result(string str1, string str2){
   int n = str1.length(); // size of the first string
   int m = str2.length(); // size of the second string   
   
   // creating vector to store the dynamic programming results 
   vector<vector<int>>dp(n+1, vector<int>(m+1)); 
   int ans = 0; // variable to store the result 
   
   // traversing over the strings using nested for loops 
   for (int i = 1; i <= n; i++){
      for (int j = 1; j <= m; j++){ 
      
         // if current elements of both the string are equal 
         if (str1[i - 1] == str2[j - 1]){
            // getting one maximum of the last two 
            dp[i][j] = 1 + dp[i - 1][j - 1];
            ans = max(ans, dp[i][j]);
         }
      }
   }
   return ans; // return the final answer or count 
} 
int main(){
   string str1 = "10110";
   string str2 = "11101"; 
   
   // calling the function
   cout<<"The maximum score for a given function by selecting equal length substrings from given binary strings is "<< result(str1,str2)<<endl;
   return 0;
}

Output

The maximum score for a given function by selecting equal length substrings from given binary strings is 3

Time and Space Complexity

The time complexity of the above code is O(N^2) as we are using the nested for loop with the N iterations each.

The space complexity of the above code is O(N^2) as we have used a 2-D array to store the elements.

Conclusion

In this tutorial, we have implemented a code for the maximum score for a given function by selecting equal-length substrings from given binary strings. We have discussed the naive approach and that is highly inefficient. According to the given function, the lesser the xor high the value, so we will make xor zero by getting the longest common substrings in O(N^2) time complexity.

Updated on: 26-Jul-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements