查找数组中不存在的给定大小的二进制排列
给定一个正整数N和一个大小为K的数组arr[]由二进制字符串组成,其中每个字符串的大小为N ,任务是找到数组arr[]中不存在的所有大小为N的二进制字符串。
例子:
Input: N = 3, arr[] = {“101”, “111”, “001”, “010”, “011”, “100”, “110”}
Output: 000
Explanation: Only 000 is absent in the given array.
Input: N = 2, arr[] = {“00”, “01”}
Output: 10 11
方法:给定的问题可以通过使用回溯查找所有大小为N的二进制字符串来解决,然后打印数组arr[]中不存在的那些字符串。请按照以下步骤解决问题:
- 初始化一个 unordered_set字符串S ,它将所有字符串存储在数组arr[]中。
- 初始化一个变量,比如total并将其设置为2 N的幂,其中N是字符串的大小。
- 使用变量i遍历范围[0, total)并执行以下步骤:
- 将空字符串变量num初始化为“” 。
- 使用变量j在范围[N – 1, 0]上进行迭代,如果i和2 j的按位与的值为true ,则将字符1推入字符串num 。否则,推入字符0 。
- 如果集合S中不存在num ,则将字符串num作为结果字符串之一打印。
- 完成上述步骤后,如果数组中存在所有可能的二进制字符串,则打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find a Binary String of
// same length other than the Strings
// present in the array
void findMissingBinaryString(vector& nums, int N)
{
unordered_set s;
int counter = 0;
// Map all the strings present in
// the array
for (string str : nums) {
s.insert(str);
}
int total = (int)pow(2, N);
string ans = "";
// Find all the substring that
// can be made
for (int i = 0; i < total; i++) {
string num = "";
for (int j = N - 1; j >= 0; j--) {
if (i & (1 << j)) {
num += '1';
}
else {
num += '0';
}
}
// If num already exists then
// increase counter
if (s.find(num) != s.end()) {
continue;
counter++;
}
// If not found print
else {
cout << num << ", ";
}
}
// If all the substrings are present
// then print -1
if (counter == total) {
cout << "-1";
}
}
// Driver Code
int main()
{
int N = 3;
vector arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find a Binary String of
// same length other than the Strings
// present in the array
static void findMissingBinaryString(String[] nums, int N)
{
HashSet s = new HashSet();
int counter = 0;
// Map all the Strings present in
// the array
for (String str : nums) {
s.add(str);
}
int total = (int)Math.pow(2, N);
String ans = "";
// Find all the subString that
// can be made
for (int i = 0; i < total; i++) {
String num = "";
for (int j = N - 1; j >= 0; j--) {
if ((i & (1 << j))>0) {
num += '1';
}
else {
num += '0';
}
}
// If num already exists then
// increase counter
if (s.contains(num)) {
counter++;
continue;
}
// If not found print
else {
System.out.print(num+ ", ");
}
}
// If all the subStrings are present
// then print -1
if (counter == total) {
System.out.print("-1");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
String []arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 program for the above approach
from math import pow
# Function to find a Binary String of
# same length other than the Strings
# present in the array
def findMissingBinaryString(nums, N):
s = set()
counter = 0
# Map all the strings present in
# the array
for x in nums:
s.add(x)
total = int(pow(2, N))
ans = ""
# Find all the substring that
# can be made
for i in range(total):
num = ""
j = N - 1
while(j >= 0):
if (i & (1 << j)):
num += '1'
else:
num += '0'
j -= 1
# If num already exists then
# increase counter
if (num in s):
continue
counter += 1
# If not found print
else:
print(num,end = ", ")
# If all the substrings are present
# then print -1
if (counter == total):
print("-1")
# Driver Code
if __name__ == '__main__':
N = 3
arr = ["101", "111", "001", "011", "100", "110"]
findMissingBinaryString(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find a Binary String of
// same length other than the Strings
// present in the array
static void findMissingBinaryString(string[] nums,
int N)
{
HashSet s = new HashSet();
int counter = 0;
// Map all the Strings present in
// the array
foreach(string str in nums) { s.Add(str); }
int total = (int)Math.Pow(2, N);
// string ans = "";
// Find all the subString that
// can be made
for (int i = 0; i < total; i++) {
string num = "";
for (int j = N - 1; j >= 0; j--) {
if ((i & (1 << j)) > 0) {
num += '1';
}
else {
num += '0';
}
}
// If num already exists then
// increase counter
if (s.Contains(num)) {
counter++;
continue;
}
// If not found print
else {
Console.Write(num + ", ");
}
}
// If all the subStrings are present
// then print -1
if (counter == total) {
Console.Write("-1");
}
}
// Driver Code
public static void Main(string[] args)
{
int N = 3;
string[] arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
}
}
// This code is contributed by ukasp.
Javascript
输出:
000, 010,
时间复杂度: O(N*2 N )
辅助空间: O(K),其中 K 是数组的大小