📌  相关文章
📜  查找数组中不存在的给定大小的二进制字符串的任何排列

📅  最后修改于: 2022-05-13 01:56:06.038000             🧑  作者: Mango

查找数组中不存在的给定大小的二进制字符串的任何排列

给定一个包含N个不同二进制字符串的数组arr[] ,每个字符串都有N个字符,任务是找到任何具有N个字符的二进制字符串,这样它就不会出现在给定数组arr[]中。

例子:

朴素的方法:给定的问题可以通过生成所有具有N位的二进制字符串并返回第一个字符串来解决,这样它就不会出现在给定的数组字符串 []中。

时间复杂度: O(2 N * N 2 )
辅助空间: O(N)

有效方法:给定的问题可以通过使用类似于康托尔对角线参数的方法进行优化。可以观察到,生成的字符串必须至少有一位不同于arr[]中的所有字符串。因此,生成的二进制字符串可以通过将第 1字符串的第 1元素的补码作为第 1元素,将第 2字符串的第 2元素的补码作为第 2元素,以此类推.以下是要遵循的步骤:

  • 创建一个字符串ans ,它存储结果字符串。最初,它是空的。
  • 以对角线顺序遍历给定的字符串,即1 个字符串的第2字符串元素,依此类推,并将当前索引处的值的补码附加到字符串ans中。
  • 遍历完整数组arr[]后存储在ans中的字符串是所需的有效字符串之一。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to find a binary string of
// N bits that does not occur in the
// given array arr[]
string findString(vector& 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 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


输出:
000

时间复杂度: O(N)
辅助空间: O(1)