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

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "0110", n = 3
<strong>Output:</strong> true
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "0110", n = 4
<strong>Output:</strong> false
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    bool queryString(string s, int n) {
        int N = s.size();
        if (n > N * (1 + N) / 2) return false;

        unordered_set<int> nums;
        int m = (int) log2(n);
        for (int i = 0; i < N; i++) {
            int num = 0;
            for (int j = max(0, i - m); j <= i; j++) {
                num = (num << 1) + s[j] - '0';
            }
            if (num > 0 && num <= n) nums.insert(num);
        }

        return nums.size() == n;
    }
};

/**
 * M = max;
 * if (Sr == ((M + 1) >> 1) && r == M & 1 ^ 1) Sr + 1 -> M + 1
 * if (Sl == M + 1 - (1 << (int)log10(Sl))) 1 + Sl -> M + 1
 */
8400