-
Notifications
You must be signed in to change notification settings - Fork 613
/
1016.py
39 lines (28 loc) · 813 Bytes
/
1016.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''
Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = "0110", N = 3
Output: true
Example 2:
Input: S = "0110", N = 4
Output: false
Note:
1 <= S.length <= 1000
1 <= N <= 10^9
'''
class Solution(object):
def queryString(self, S, N):
"""
:type S: str
:type N: int
:rtype: bool
"""
for num in range(1, N+1):
binary_str = ''
while (num != 0):
binary_str += str(num%2)
num /= 2
reversed_str = binary_str[::-1]
if reversed_str not in S:
return False
return True