March LeetCoding Challenge 2021 — Day 12: Check If a String Contains All Binary Codes of Size K
Tags: algorithms, codenewbie, interview, leetcode
Published: 2021-03-13 09:53:15


Today, we will solve the 12 th problem of the March LeetCoding Challenge.


## Problem Statement

Given a binary string s and an integer k.

Return *True* if every binary code of length k is a substring of s. Otherwise, return *False*.

**Example 1:**

    **Input:** s = "00110110", k = 2
    **Output:** true
    **Explanation:** The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.

**Example 2:**

    **Input:** s = "00110", k = 2
    **Output:** true

**Example 3:**

    **Input:** s = "0110", k = 1
    **Output:** true
    **Explanation:** The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.

**Example 4:**

    **Input:** s = "0110", k = 2
    **Output:** false
    **Explanation:** The binary code "00" is of length 2 and doesn't exist in the array.

**Example 5:**

    **Input:** s = "0000000001011100", k = 4
    **Output:** false

## Solution

In this problem, we are asked to find whether we have all the binary codes of size k in a given string.

We know that for a given size k, we can have a total of 2^k numbers. We need to check whether all of those 2^k numbers are present in the given string. For that, we can use a HashSet , to store all the values which we have found out. In the end, we can compare the size of HashSet and 2^k , if they are equal then return true else return false.

The code is given below.
{% gist https://gist.github.com/sksaikia/5e85be07db2a0324c9eb2f4f2fcd17ae.js %}
Time Complexity: O(n)

Space Complexity: O(n)

The code can be found here

{%github sksaikia/Leetcode %}

Check out my other posts on March LeetCoding Challenge 2021.

 1. [March LeetCoding Challenge — Day 1 — Distribute Candies](https://medium.com/dev-genius/march-leetcoding-challenge-2021-problem-1-distribute-candies-f37f66ea7ee9)

 2. [March LeetCoding Challenge — Day 2 — Set Mismatch](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-2-set-mismatch-4abd5ee491c9)

 3. [March LeetCoding Challenge — Day 3 — Missing Number](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-3-missing-number-ae8ee45a58cb)

 4. [March LeetCoding Challenge — Day 4 — Intersection of Two Linked Lists](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-4-intersection-of-two-linked-lists-a775449b5563)

 5. [March LeetCoding Challenge — Day 5 — Average of Levels in Binary Tree](https://link.medium.com/sC9L595opeb)

 6. [March LeetCoding Challenge — Day 6 — Short Encoding of Words](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-6-short-encoding-of-words-7fed4bfae557)

 7. [March LeetCoding Challenge — Day 8 — Remove Palindromic Subsequences](https://sourav-saikia.medium.com/march-leetcoding-challenge-2021-day-8-remove-palindromic-subsequences-12b037705722)

 8. [March LeetCoding Challenge — Day 10 — Integer to Roman](https://medium.com/leetcode-simplified/march-leetcoding-challenge-2021-day-10-integer-to-roman-76caa87a0e07)