# 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 2^{4}=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 2^{k} 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<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*

*filter_none*

Output:

Yes

## Recommended Posts:

- Count of cyclic permutations having XOR with other binary string as 0
- Number of unique permutations starting with 1 of a Binary String
- Check if given string can be formed by two other strings or their permutations
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Check if a string contains a palindromic sub-string of even length
- Binary String of given length that without a palindrome of size 3
- Length of longest consecutive ones by at most one swap in a Binary String
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check length of a string is equal to the number appended at its last
- Check if the frequency of any character is more than half the length of the string
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Python | Check if a given string is binary string or not
- Check if a binary string contains consecutive same or not

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.