Talentd
FleetCode

1016. Binary String With Substrings Representing 1 To N

Medium58.5% acceptance2 companies

Given a binary string s and a positive integer n, return true if the binary representation of all the integers in the range [1, n] are substrings of s, or false otherwise.

A substring is a contiguous sequence of characters within a string.

Example 1:

Input: s = "0110", n = 3
Output: true

Example 2:

Input: s = "0110", n = 4
Output: false

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either '0' or '1'.
  • 1 <= n <= 109

Asked at 2 companies

Topics

Hints open one at a time

Hint 1
We only need to check substrings of length at most 30, because 10^9 has 30 bits.
16:53
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def queryString(self, s: str, n: int) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
"0110"
3
Input
"0110"
4
Your code is saved on this device.Solution