📌  相关文章
📜  检查二进制字符串包含长度为k的所有排列

📅  最后修改于: 2021-04-23 06:37:40             🧑  作者: Mango

给定一个二进制字符串和k,以检查它是否包含长度为k的所有排列。
例子:

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 all possibilities of
binary sequences with k = 2. Here 11 
sequence is missing

方法1:
解释:
在此示例中,未找到一个长度为k的二进制序列,它是0110。
因此,所有k = 4的二进制序列将为2 4 = 16。他们正在关注
0000、0001、0010、0011、0100、0101、0110、0111,
1000、1001、1010、1011、1100、1101、1110、1111
全部应为给定二进制字符串的子字符串,然后打印是,否则否
算法
取二进制字符串和大小k。在二进制字符串,我们检查二进制序列是否匹配。二进制序列的大小为k,因为我们知道在二进制中使用0和1位数字,因此生成总的二进制子集是2 k个元素。其背后的主要思想是将list中的所有二进制值存储为字符串,然后将list的所有项目与给定的二进制字符串进行比较作为子集。如果所有内容都出现在二进制字符串,则打印“是”,否则打印“否”。

JAVA
// Java program to Check a binary string
// contains all permutations of length k.
 
import java.util.*;
public class Checkbinary {
 
    // to check all Permutation in given String
    public static boolean tocheck(String s, int k)
    {
        List list = BinarySubsets(k);
 
        // to check binary sequences are available
        // in string or not
        for (String b : list)
            if (s.indexOf(b) == -1)
                return false;       
 
        return true;
    }
 
    // to generate all binary subsets of given length k
    public static List BinarySubsets(int k)
    {
        // Declare the list as String
        List list = new ArrayList<>();
 
        // to define the format of binary of
        // given length k
        String format = "%0" + k + "d";
 
        // returns the string representation of the
        // unsigned integer value represented by
        // the argument in binary (base 2)  using
        // Integer.toBinaryString and convert it
        // into integer using Integer.valueOf.
        // Loop for 2k elements
        for (int i = 0; i < Math.pow(2, k); i++)
        {
            // To add in the list all possible
            // binary sequence of given length
            list.add(String.format(format,
                Integer.valueOf(Integer.toBinaryString(i))));
 
            /* To Show all binary sequence of given
               length k
            System.out.println(String.format(format,
            Integer.valueOf(Integer.toBinaryString(i))));*/
        }
        return list;
    }
 
    // drive main
    public static void main(String[] args)
    {
        String str = "11001";
        int num = 2;
        if (tocheck(str, num))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


C++
// C++ Program to Check If a
// String Contains All Binary
// Codes of Size K
#include 
using namespace std;
#define int long long
 
bool hasAllcodes(string s, int k)
{
     
    // Unordered map of type string
    unordered_set us;
   
    for (int i = 0; i + k <= s.size(); i++)
    {
        us.insert(s.substr(i, k));
    }
    return us.size() == 1 << k;
}
 
// Driver Code
signed main()
{
    string s = "00110110";
    int k = 2;
    if (hasAllcodes)
    {
        cout << "YES\n";
    }
    else
    {
        cout << "NO\n";
    }
}


Java
// Java Program to Check If a
// String Contains All Binary
// Codes of Size K
import java.io.*;
import java.util.*;
class GFG
{
    static boolean hasAllcodes(String s, int k)
    {
       
        // Unordered map of type string
        Set us= new HashSet();
        for(int i = 0; i + k <= s.length(); i++)
        {
            us.add(s.substring(i, i + k));
        }
        return (us.size() == (1 << k));
    }
   
    // Driver code
    public static void main (String[] args)
    {
        String s = "00110110";
        int k = 2;
        if(hasAllcodes(s, k))
        {
            System.out.println("YES");
        }
        else
        {
            System.out.println("NO");
        }
    }
}
 
//  This code is contributed by avanitrachhadiya2155


Python3
# Python3 Program to Check If a
# String Contains All Binary
# Codes of Size K
def hasAllcodes(s, k) :
     
    # Unordered map of type string
    us = set()
    for i in range(len(s) + 1) :   
        us.add(s[i : k])   
    return len(us) == 1 << k
 
# Driver code
s = "00110110"
k = 2
if (hasAllcodes) :
    print("YES")
else :
    print("NO")
 
    # This code is contributed by divyeshrabadiya07


C#
// C# Program to Check If a
// String Contains All Binary
// Codes of Size K
using System;
using System.Collections.Generic;
class GFG {
 
  static bool hasAllcodes(string s, int k)
  {
 
    // Unordered map of type string
    HashSet us = new HashSet();
 
    for (int i = 0; i + k <= s.Length; i++)
    {
      us.Add(s.Substring(i, k));
    }
 
    return us.Count == 1 << k;
  }
 
  // Driver code
  static void Main()
  {
    string s = "00110110";
    int k = 2;
    if(hasAllcodes(s, k))
    {
      Console.WriteLine("YES");
    }
    else
    {
      Console.WriteLine("NO");
    }
  }
}
 
// This code is contributed by divyesh072019


输出
Yes

方法2:
算法
取二进制字符串和大小k。在二进制字符串,我们检查二进制序列是否匹配。二进制序列的大小为k,因为我们知道在二进制中使用0和1位数字,因此生成总的二进制子集是2 k个元素。其背后的主要思想是将给定字符串的大小为k的所有子字符串存储到集合中,即存储大小为k的不同子字符串。如果组的大小等于2 k ,则打印“是”,否则打印“否”。

C++

// C++ Program to Check If a
// String Contains All Binary
// Codes of Size K
#include 
using namespace std;
#define int long long
 
bool hasAllcodes(string s, int k)
{
     
    // Unordered map of type string
    unordered_set us;
   
    for (int i = 0; i + k <= s.size(); i++)
    {
        us.insert(s.substr(i, k));
    }
    return us.size() == 1 << k;
}
 
// Driver Code
signed main()
{
    string s = "00110110";
    int k = 2;
    if (hasAllcodes)
    {
        cout << "YES\n";
    }
    else
    {
        cout << "NO\n";
    }
}

Java

// Java Program to Check If a
// String Contains All Binary
// Codes of Size K
import java.io.*;
import java.util.*;
class GFG
{
    static boolean hasAllcodes(String s, int k)
    {
       
        // Unordered map of type string
        Set us= new HashSet();
        for(int i = 0; i + k <= s.length(); i++)
        {
            us.add(s.substring(i, i + k));
        }
        return (us.size() == (1 << k));
    }
   
    // Driver code
    public static void main (String[] args)
    {
        String s = "00110110";
        int k = 2;
        if(hasAllcodes(s, k))
        {
            System.out.println("YES");
        }
        else
        {
            System.out.println("NO");
        }
    }
}
 
//  This code is contributed by avanitrachhadiya2155

Python3

# Python3 Program to Check If a
# String Contains All Binary
# Codes of Size K
def hasAllcodes(s, k) :
     
    # Unordered map of type string
    us = set()
    for i in range(len(s) + 1) :   
        us.add(s[i : k])   
    return len(us) == 1 << k
 
# Driver code
s = "00110110"
k = 2
if (hasAllcodes) :
    print("YES")
else :
    print("NO")
 
    # This code is contributed by divyeshrabadiya07

C#

// C# Program to Check If a
// String Contains All Binary
// Codes of Size K
using System;
using System.Collections.Generic;
class GFG {
 
  static bool hasAllcodes(string s, int k)
  {
 
    // Unordered map of type string
    HashSet us = new HashSet();
 
    for (int i = 0; i + k <= s.Length; i++)
    {
      us.Add(s.Substring(i, k));
    }
 
    return us.Count == 1 << k;
  }
 
  // Driver code
  static void Main()
  {
    string s = "00110110";
    int k = 2;
    if(hasAllcodes(s, k))
    {
      Console.WriteLine("YES");
    }
    else
    {
      Console.WriteLine("NO");
    }
  }
}
 
// This code is contributed by divyesh072019
输出
YES