# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// 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"``); ` `    ``} ` `} `

Output:

`Yes`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Improved By : nidhi_biet