↓Skip to main content
  1. LeetCode/

LeetCode 1461: Check If a String Contains All Binary Codes of Size K

·2 mins· ·
LeetCode Medium Hash-Table String Bit-Manipulation Rolling-Hash
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-05

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

There are 2^k binary codes of length k. Put every length-k substring of s in a set (String Template 3); a size of 2^k means all are present.

  • There are only len(s) - k + 1 substrings; fewer than 2^k means it cannot work, so return early.
  • With k <= 20, a rolling integer can replace substrings: code = ((code << 1) | bit) & mask, avoiding slices.

My Attempts
#

  1. Attempt 1 added s[right - k: right], which excludes right, so the last window was never collected. For "10", k = 1 it only collected "1" and returned False; the answer is True.
  2. Attempt 2 changed it to s[right - k - 1: right], length k + 1, which is worse.
  3. s[right - k + 1: right + 1] passed.

Solution
#

class Solution:
    def hasAllCodes(self, s: str, k: int) -> bool:
        need = 1 << k
        if len(s) - k + 1 < need:
            return False
        seen = {s[i:i + k] for i in range(len(s) - k + 1)}
        return len(seen) == need

Complexity
#

Time O(n · k), space O(min(n, 2^k) · k). A rolling integer brings time down to O(n).

Mistakes to Avoid
#

  • s[a:b] has length b - a and excludes b.
  • With fewer than 2^k substrings, return False immediately.

Interview Explanation
#

I put every length-k substring in a set and check whether its size is 2^k; if there are fewer substrings than 2^k the answer is False right away. A rolling bitmask of the current k bits avoids slicing. Time O(nk).

Related

LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·2 mins
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 note: pair each word with its reverse on both sides; self-palindromes also pair with each other, and only one leftover can sit in the middle
LeetCode 1410: HTML Entity Parser
·2 mins
LeetCode Medium Hash-Table String
LeetCode 1410 note: scan left to right, trying all six entities at each position and skipping past a match; chained replace does not work
LeetCode 3121: Count the Number of Special Characters II
·2 mins
LeetCode Medium Hash-Table String
LeetCode 3121 note: track each letter’s last lowercase index and first uppercase index; it counts only when the former comes first