Algorithm
Problem Name: 1016. Binary String With Substrings Representing 1 To N
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
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
const queryString = function(S, N) {
for(let i = 1; i < = N; i++) {
let tmp = bin(i)
if(S.indexOf(tmp) === -1) return false
}
return true
};
function bin(num) {
return (num >>> 0).toString(2)
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Python Programming
Code -
Python Programming
class Solution:
def queryString(self, S: str, N: int) -> bool:
return not set(range(1, N + 1)) - {int(S[i:j + 1], 2) for i in range(len(S)) for j in range(i, len(S))}
Copy The Code &
Try With Live Editor
Input
Output