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

Explanation:
In this example one binary sequence of length k is not found it is 0110.



So all binary sequences with k=4 will be 24=16. they are following
0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111,
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
All should be sub string of given binary string then print Yes otherwise No

Algorithm
Taking binary string and the size k. In binary string we check binary sequences are matched or not. Binary sequence is made of size k, as we know that in binary using 0 and 1 digit so to generate total binary subsets is 2k element. The main idea behind it, to store all binary value in list as string and then compare list all item to given binary string as subset. If all are occur inside the binary string then print “Yes” otherwise print “No”.

JAVA

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String> 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<String> BinarySubsets(int k)
    {
        // Decalre the list as String
        List<String> 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 2<sup>k</sup> 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");
    }
}

chevron_right



Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.