Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Check If a String Contains All Binary Codes of Size K

XPChallenge Points: 20
levelLevel: Medium

You are given a binary string s and an integer k.

Your task is to determine whether every possible binary code of length k appears as a substring in s.

Return:

  • true if all possible binary codes of size k exist as substrings of s,

  • otherwise, return false.

Example 1:

Input: s = "0110" k = 1

Output: true

Explanation:

All binary codes of length 1 → "0", "1" Both exist in the string, so true.

Example 2:

Input: s = "00110110" k = 2

Output: true

Explanation:

All binary codes of length 2 are: "00" → found at index 0 "01" → found at index 1 "10" → found at index 3 "11" → found at index 2 Since all are present, return true.

Example 3:

Input: s = "0110" k = 2

Output: false

Explanation:

Binary codes of length 2 are "00", "01", "10", "11". The substring "00" does not appear → false.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result