<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>

#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    vector<int> fillLPS(string needle) {
        int n = needle.size();
        vector<int> lps(n, 0);
        int i = 1, len = 0;
        while (i < n) {
            if (needle[i] == needle[len]) {
                len++;
                lps[i] = len;
                i++;
            } else {
                if (len == 0) {
                    lps[i] = 0;
                    i++;
                } else {
                    len = lps[len - 1];
                }
            }
        }
        return lps;
    }

    bool strStr(string haystack, string needle) {
        int n = haystack.size(), m = needle.size();
        vector<int> lps = fillLPS(needle);
        int i = 0, j = 0;
        while (i < n) {
            if (haystack[i] == needle[j]) {
                i++;
                j++;
                if (j == m) {
                    return true;
                }
            } else if (i < n && haystack[i] != needle[j]) {
                if (j == 0) {
                    i++;
                } else {
                    j = lps[j - 1];
                }
            }
        }
        return false;
    }

    bool queryString(string s, int n) {
        for (int i = 1; i <= n; i++) {
            string binaryString;
            int temp = i;
            while (temp > 0) {
                binaryString = (temp % 2 == 0 ? "0" : "1") + binaryString;
                temp /= 2;
            }
            if (strStr(s, binaryString) == false) return false;
        }
        return true;
    }
};

8500