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:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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 missingMethod 1:
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<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) { // Declare 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"); }} |
Yes
Method 2:
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 the substring of size k of the given string to the set i.e. storing the distinct substring of size k. If the size of the set is equal to 2k than print “YES” otherwise print “NO”.
C++14
// C++ Program to Check If a// String Contains All Binary// Codes of Size K#include <bits/stdc++.h>using namespace std;#define int long longbool hasAllcodes(string s, int k){ // Unordered map of type string unordered_set<string> us; for (int i = 0; i + k <= s.size(); i++) { us.insert(s.substr(i, k)); } return us.size() == 1 << k;}// Driver Codesigned 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 Kimport java.io.*;import java.util.*;class GFG{ static boolean hasAllcodes(String s, int k) { // Unordered map of type string Set<String> us= new HashSet<String>(); 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 Kdef 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 codes = "00110110"k = 2if (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 Kusing System;using System.Collections.Generic;class GFG { static bool hasAllcodes(string s, int k) { // Unordered map of type string HashSet<string> us = new HashSet<string>(); 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 |
Javascript
<script>// Javascript program to Check If a// String Contains All Binary// Codes of Size Kfunction hasAllcodes(s,k){ // Unordered map of type string let us = new Set(); for(let i = 0; i + k <= s.length; i++) { us.add(s.substring(i, i + k)); } return (us.size == (1 << k));}// Driver codelet s = "00110110";let k = 2; if (hasAllcodes(s, k)){ document.write("YES");}else{ document.write("NO");}// This code is contributed by ab2127</script> |
YES


