Find any permutation of Binary String of given size not present in Array
Given an array arr[] of N distinct binary strings each having N characters, the task is to find any binary string having N characters such that it doesn’t occur in the given array arr[].
Example:
Input: arr[] = {“10”, “01”}
Output: 00
Explanation: String “00” does not appear in array arr[]. Another valid string can be “11” which does not occur in the array arr[] as well.
Input: arr[] {“111”, “011”, “001”}
Output: 101
Explanation: String “101” does not appear in array arr[]. Another valid strings are “000”, “010”, “100”, and “110”.
Naive Approach: The given problem can be solved by generating all the binary strings having N bits and returning the 1st string such that it does not occur in the given array arr[].
Time Complexity: O(2N * N2)
Auxiliary Space: O(N)
Efficient Approach: The given problem can be optimized by using a method similar to Cantor’s Diagonal Argument. It can be observed that the resulting string must have at least one bit that is different from all the strings in arr[]. Therefore, the resulting binary string can be constructed by taking the complement of the 1st element of the 1st string as the 1st element, the complement of the 2nd element of the 2nd string as the 2nd element, and so on. Below are the steps to follow:
- Create a string ans, which stores the resulting string. Initially, it is empty.
- Iterate through the given strings in a diagonal order, i.e, 1st element of 1st string, 2nd element of 2nd string, and so on, and append the compliment of the value at the current index into the string ans.
- The string stored in ans after iterating through the complete array arr[] is one of the valid strings required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findString(vector<string>& arr, int N)
{
string ans = "" ;
for ( int i = 0; i < N; i++) {
ans += arr[i][i] == '0' ? '1' : '0' ;
}
return ans;
}
int main()
{
vector<string> arr{ "111" , "011" , "001" };
int N = arr.size();
cout << findString(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String findString(String arr[], int N)
{
String ans = "" ;
for ( int i = 0 ; i < N; i++) {
ans += arr[i].charAt(i) == '0' ? '1' : '0' ;
}
return ans;
}
public static void main (String[] args) {
String arr[] = { "111" , "011" , "001" };
int N = arr.length;
System.out.println(findString(arr, N));
}
}
|
Python3
def findString(arr, N):
ans = ""
for i in range (N):
ans + = '1' if arr[i][i] = = '0' else '0'
return ans
if __name__ = = '__main__' :
arr = [ "111" , "011" , "001" ]
N = len (arr)
print (findString(arr, N))
|
C#
using System;
class GFG {
static string findString( string [] arr, int N)
{
string ans = "" ;
for ( int i = 0; i < N; i++) {
ans += arr[i][i] == '0' ? '1' : '0' ;
}
return ans;
}
public static void Main(String[] args)
{
string [] arr = { "111" , "011" , "001" };
int N = arr.Length;
Console.WriteLine(findString(arr, N));
}
}
|
Javascript
<script>
function findString(arr, N)
{
let ans = "" ;
for (let i = 0; i < N; i++) {
ans += arr[i][i] == '0' ? '1' : '0' ;
}
return ans;
}
let arr = [ "111" , "011" , "001" ];
let N = arr.length;
document.write(findString(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
18 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...