Find Binary String of size at most 3N containing at least 2 given strings of size 2N as subsequences


We are given three strings of equal size equal to 2*N, where N is an integer. We have to create a string of the size 3*N and at least two strings from the given strings be the subsequence of it. Also, the given strings are binary strings which means they only contain two different characters '0' and '1'. We will implement a code by traversing over the string and getting the frequency of the zeros and ones.

Sample Examples

Input

string str1 = “11”;
string str2 = “10”;
string str3 = “10”; 

Output

110

Explanation: First two strings are the sequence of the given output string.

Input

string str1 = “001111”;
string str2 = “001111”;
string str3 = “11000”; 

Output

00001111

Observation

In the given problem we have binary strings of even length, which means there are only two different characters. Here we have only two characters so either both of the characters have the same frequency of N or one of the characters has more frequency than N and the other will have less frequency in N.

For example: If the frequency of '0' in string1 is more than N, and the frequency of '1' in string2 is more than N. Or, another case is vice-versa of it, in which string2 has a frequency of '0' greater than N and string1 has a frequency of '1' greater than N. Now, the third string will have either frequency of '1' that is greater than or equal to N which makes it shares the same frequency of 1 with either of one string or in the case of '0' also it shares the same frequency of '0' with one of the given string.

So, the point is there will always be two strings present with the frequency of any one character more than or equal to N. So, the final string is always possible to make.

Approach

In this approach, we are going to create a function in which we will check the frequency of the one and zero for all three given strings.

For any two strings which have the same higher frequency of any character present, we will take them and the character with the highest frequency and pass them to another function to calculate the required string.

We will use the two-pointers technique to get all the less frequent characters first and then more frequent characters.

Example

#include <bits/stdc++.h>
using namespace std;

// function to build the string 
string buildString(string str1, string str2, char req){
   // using the two pointers approach  
   int ptr1 = 0, ptr2 = 0;
   int len = str1.size();
   string ans = "";
   while(ptr1 < len && ptr2 < len){
      while(ptr1 < len && str1[ptr1] != req){
         ans += str1[ptr1];
         ptr1++;
      }
      while(ptr2 < len && str2[ptr2] != req){
         ans += str2[ptr2];
         ptr2++;
      }
      // if we are at end break
      if(ptr1 == len || ptr2 == len){
         break;
      }
      if(str1[ptr1] == str2[ptr2]){
         ans += str1[ptr1];
         ptr1++, ptr2++;
      } else{
         ans += str1[ptr1];
         ptr1++;
      }
   }
   // adding the remaining characters 
   while(ptr1 < len){
      ans += str1[ptr1];
      ptr1++;
   }
   while(ptr2 < len){
      ans += str2[ptr2];
   }
   return ans;
}
// function to get the string 
string getString(string str1, string str2, string str3){
   // getting length of the given strings as given the size of all the strings is same 
   int len = str1.size();
   // creating arrays to store the frequency of the zeroes
   int freq[3] = {0};
   // getting values for the first string
   for(int i=0; i<len; i++){
      if(str1[i] == '0'){
         freq[0]++;
      }
   }
   // getting values for the second string
   for(int i=0; i<len; i++){
      if(str2[i] == '0'){
         freq[1]++;
      }
   }
   // getting values for the third string
   for(int i=0; i<len; i++){
      if(str3[i] == '0'){
         freq[2]++;
      }
   }
   char req; 
   // conditions to make the combination of the two string 
   if((freq[0] >= len/ 2 && freq[1] >= len/2) || (freq[0] <= len/ 2 && freq[1] <= len/2) ){
      // frequency of zero or one is more for both first and second string leave the third string away 
      if(freq[0] >= len/2 && freq[1]>= len/2){
         req = '0';
      }
      else{
         req = '1';
      }
   }
   else if((freq[0] >= len/ 2 && freq[2] >= len/2) || (freq[0] <= len/ 2 && freq[2] <= len/2) ){
      // frequency of zero or one is more for both first and third string leave the second string away 
      str2 = str3;
      if(freq[0] >= len/2 && freq[2] >= len/2){
         req = '0';
      } else{
         req = '1';
      }
   } else{
      // both initial conditions are false means both second and third string have the frequency of zero or one in same side leave the first string away 
      str1 = str3;
      if(freq[2] >= len/2 && freq[1] >= len/2){
         req = '0';
      } else{
         req = '1';
      }
   }
   // calling function to return the requird string
   return buildString(str1, str2, req);
}
int main(){
   string str1 = "001111"; // given strings
   string str2 = "001111";
   string str3 = "001100";
   // getting the required string 
   cout<<"The required string of at most size 3*N is: " << getString(str1, str2, str3 )<<endl;
}

Output

The required string of at most size 3*N is: 00001111

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given length, as we are moving over the string just once.

The space complexity of the above code is O(N), which is used to store the final answer.

Conclusion

In this program, we have implemented a code to get a string of size 3*N which is the super-sequence of any two strings among the given three binary strings of the length 2*N. We have used the pigeonhole principle to proof that the answer will always be present and then by using the two pointers and counting the frequency we have created the required string with time and space complexity of O(N).

Updated on: 31-Aug-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements