Find any permutation of Binary String of given size not present in Array
Last Updated :
18 Jan, 2022
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++
// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find a binary string of
// N bits that does not occur in the
// given array arr[]
string findString(vector<string>& arr, int N)
{
// Stores the resultant string
string ans = "";
// Loop to iterate over all the given
// strings in a diagonal order
for (int i = 0; i < N; i++) {
// Append the complement of element
// at current index into ans
ans += arr[i][i] == '0' ? '1' : '0';
}
// Return Answer
return ans;
}
// Driver code
int main()
{
vector<string> arr{ "111", "011", "001" };
int N = arr.size();
cout << findString(arr, N);
return 0;
}
Java
// Java Program for the above approach
import java.io.*;
class GFG {
// Function to find a binary string of
// N bits that does not occur in the
// given array arr[]
static String findString(String arr[], int N)
{
// Stores the resultant string
String ans = "";
// Loop to iterate over all the given
// strings in a diagonal order
for (int i = 0; i < N; i++) {
// Append the complement of element
// at current index into ans
ans += arr[i].charAt(i) == '0' ? '1' : '0';
}
// Return Answer
return ans;
}
// Driver code
public static void main (String[] args) {
String arr[] = { "111", "011", "001" };
int N = arr.length;
System.out.println(findString(arr, N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python 3 Program for the above approach
# Function to find a binary string of
# N bits that does not occur in the
# given array arr[]
def findString(arr, N):
# Stores the resultant string
ans = ""
# Loop to iterate over all the given
# strings in a diagonal order
for i in range(N):
# Append the complement of element
# at current index into ans
ans += '1' if arr[i][i] == '0' else '0'
# Return Answer
return ans
# Driver code
if __name__ == '__main__':
arr = ["111", "011", "001"]
N = len(arr)
print(findString(arr, N))
# This code is contributed by bgangwar59.
C#
// C# Program for the above approach
using System;
class GFG {
// Function to find a binary string of
// N bits that does not occur in the
// given array arr[]
static string findString(string[] arr, int N)
{
// Stores the resultant string
string ans = "";
// Loop to iterate over all the given
// strings in a diagonal order
for (int i = 0; i < N; i++) {
// Append the complement of element
// at current index into ans
ans += arr[i][i] == '0' ? '1' : '0';
}
// Return Answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
string[] arr = { "111", "011", "001" };
int N = arr.Length;
Console.WriteLine(findString(arr, N));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to find a binary string of
// N bits that does not occur in the
// given array arr[]
function findString(arr, N)
{
// Stores the resultant string
let ans = "";
// Loop to iterate over all the given
// strings in a diagonal order
for (let i = 0; i < N; i++) {
// Append the complement of element
// at current index into ans
ans += arr[i][i] == '0' ? '1' : '0';
}
// Return Answer
return ans;
}
// Driver code
let arr = ["111", "011", "001"];
let N = arr.length;
document.write(findString(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Find Binary permutations of given size not present in the Array Given a positive integer N and an array arr[] of size K consisting of binary string where each string is of size N, the task is to find all the binary strings of size N that are not present in the array arr[]. Examples: Input: N = 3, arr[] = {"101", "111", "001", "010", "011", "100", "110"}Output: 0
7 min read
Lexicographically smallest permutation of [1, N] based on given Binary string Given a binary string S of size (N - 1), the task is to find the lexicographically smallest permutation P of the first N natural numbers such that for every index i, if S[i] equals '0' then P[i + 1] must be greater than P[i] and if S[i] equals '1' then P[i + 1] must be less than P[i]. Examples: Inpu
6 min read
Check if a binary string contains all permutations of length k Given a binary string and k, to check whether it's contains all permutations of length k or not. Examples: Input : Binary string 11001 k : 2 Output : Yes 11001 contains all possibilities of binary sequences with k = 2, 00, 01, 10, 11 Input : Binary string: 1001 k : 2 Output: No 1001 does not contain
13 min read
Distinct Numbers obtained by generating all permutations of a Binary String Given a binary string S, the task is to print all distinct decimal numbers that can be obtained by generating all permutations of the binary string. Examples: Input: S = "110"Output: {3, 5, 6}Explanation: All possible permutations are {"110", "101", "110", "101", "011", "011"}.Equivalent decimal num
8 min read
Minimum length of string having all permutation of given string. Given a string S where 1\leq length\; of\; S\leq 26 . Assume that all the characters in S are unique. The task is to compute the minimum length of a string which consists of all the permutations of the given string in any order. Note: All permutations must be present as a substring in the resulting
4 min read
Print all Unique permutations of a given string. Given a string that may contain duplicates, the task is find all unique permutations of given string in any order.Examples: Input: "ABC"Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Explanation: Given string ABC has 6 unique permutations as "ABC", "ACB", "BAC", "BCA", "CAB" and "CBA".Input: "AAA
12 min read